This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 2 years ago.
I have an array of strings: const names = ['name1', 'name2', name3']
And I need the following array of objects:
const newArray = [{ name: 'name1' }, { name: 'name2' }, { name: 'name3' }]
How can I create newArray from names, or even possibly convert names itself without creating brand new array?
This can be easily achieved with the map function.
const newArray = names.map((name) => {
return {
name
};
});
map() can do that for you:
const names = ['name1', 'name2', 'name3'];
const newArray = names.map(x=>{return{name:x};});
console.log(newArray);
Related
This question already has answers here:
Flatten array with objects into 1 object
(12 answers)
Closed 1 year ago.
I have an array of objects in this shape;
const objectsArray = [{ projects: [] }, { components: [] }];
I want to merge it into a single object like this;
const mergedObject={ projects: [], components: [] }
Is there a better way of doing it other than this:
Object.keys(objectArray).map(key=>objectArray[key]).reduce((old,item)=>(
{...old,...item}
),{})
This might be a bit simpler:
const objectsArray = [{ projects: [1,2,3] }, { components: [4,5,6] }];
let mergedObjects
objectsArray.forEach(obj => mergedObjects = { ...mergedObjects, ...obj})
console.log(mergedObjects)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Convert array of objects into array of properties [duplicate]
(4 answers)
Closed 2 years ago.
Is there a way to transform the following array of objects:
array = [
{ date: "08/18", id: 1 },
{ date: "08/14", id: 2 },
{ date: "08/15", id: 3 }
]
Into this? Only returning the dates:
array2 = ["08/18", "08/14", "08/15"]
I've been trying to push the date elements as it follows, but it doesn's seem to work:
array.map(e => {
array2.push(e.date)
})
Thanks in advance.
You can use the array map function:
array2 = array.map(object => object.date);
This question already has answers here:
Convert object to array of key–value objects like `{ name: "Apple", value: "0.6" }`
(3 answers)
Closed 1 year ago.
I'm sorry if this is redundant with other posts but I currently have the following array:
let test = {1:100, 2:200, 3:300}
But I'd like to convert this to:
test = [
{id: 1, value: 100},
{id: 2, value: 200},
{id: 3, value: 300}
]
Any help appreciated - even just pointing me to posts that solve this question :)
You can use Object.entries to get an array of key, value pairs and then map that to an array of objects:
let test = {1:100, 2:200, 3:300};
let out = Object.entries(test).map(([k, v]) => ({ id : k, value: v }));
console.log(out);
Here's a version using keys and map:
var obj = {1:100,2:200,3:300}
var result = Object.keys(obj).map((key) => ({ id: key, value: obj[key]}));
console.log(result)
Using Object.entries() and some destructuring
let test = {1:100, 2:200, 3:300}
let res = Object.entries(test).map(([id, value]) => ({id, value}))
console.log(res)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
How can i create another array by filtering one json array and include just the values pairs of a specific key?
Example (Filtering Value Pairs of Number Keys):
Array:
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]````
Result Array:
````var filteredarray = ['1234','5678'];````
Thanks!
const a = [
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]
function getNumbers(){
return a.map(item => item.Number);
}
getNumbers();
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 6 years ago.
I have an array of objects:
[{
name: "test",
age: 20,
gender: "male"
},
{
name: "test2",
age: 22,
gender: "female"
}]
Frequently I need to create a singleton array which contains a specific property from the object array above, for example extract only the names from the array above and create an array from it:
NewArray = ["test","test2"]
Currently I loop over the object array and push the property I need to the new array.
Is there a quick way to do it in Javascript/ES instead of looping every time I need to get specific property?
var people = [{
name:'test1',
age:20
}, {
name:'test2',
age:30
}]
let names = people.map(function(item) {
return item.name
});
console.log(names);