How do I convert array of objects to object in javascript?

ZarNiHein

I have an array of objects:

let userDetail = [{id: "12347442", name: "ZarNi" , age: 23}
{id: "2435457", name: "Michael" , age: 13},
{id: "8639503", name: "John" , age: 18}
]

I filtered this array into this:

let filteredUser = userDetail.filter(user => user.id === "12347442");

I need to put object to react state. so I've written like this:

this.setState({user: {...filteredUser}});

When I took the console for user state,I've got the result like this:

console.log(this.state.user);

Result => {0:{id: "12347442", name: "ZarNi" , age: 23}} as a result;

I don't want Key 0. I just want to get object like this

{id: "12347442", name: "ZarNi" , age: 23}
raina77ow

If all the records in your array have different IDs OR you expect to use the first match anyway, it's not filter you need - but find:

// const, as you're not expected to change value of that variable
const foundUser = userDetail.find(user => user.id === "12347442");

In this case you won't even have to worry about extracting the first element from that array which is result of filter op - and pass that user into setState directly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can i convert object into an array in javascript?

How do I convert array of Objects into one Object in JavaScript?

How do I convert a javascript object array to a string array of the object attribute I want?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert an object to an array?

How do I merge an array of objects in Javascript?

In javascript, I have an array of objects. How do I console log the name of the object, rather than the contents?

Given an array of json objects, how do I convert values in each object to an int or float?

I have a .log file of objects how do I convert them to a JSON object for iterating through in Javascript?

How do I convert an array of strings to an object property in javascript?

How can I convert this object of objects into an array of objects?

How to convert array of objects to object of array in javascript?

How do I convert array of Objects to one Object in JavaScript?

How do I filter an array object inside of a array of objects Javascript?

how do you convert javascript object with arrays in to array of objects with corresponding name and value?

How do you convert an array of {"name":"myName","value":"myValue"} objects into an object of "myName":"myValue" pairs in JavaScript?

How do I convert this php array into a JavaScript array of objects?

How I can convert array to object in Javascript

how do I create an array of objects with array in the object from an object with the same key in javascript

How do I convert an array into an object within array - Angular and Javascript

How Do I convert my array of objects into JSON object in C# (.net core)

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I return an array of objects that only contains a certain object in an array for Javascript?

How do I modify an object value in a cloned array of objects in Javascript

How to convert the object which contains multiple array into an array of objects in javascript?

How to convert an object to array of multiple objects in javascript

How do I create an object from array of objects using javascript?

How do i convert an array of objects into 1 object in typescript

Convert object of objects to array of objects in javascript

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive