extract sub parameters from array with objects [duplicate] - javascript

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I have next array:
let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
how I can get array without 'param'?
[{name: 'bla'},{name: 'blu'}]

let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
const result = someArr.map(el => el.param);
console.log(result);

let newArray = someArr.map(item => {
return item.param;
});

Related

How to access age value from the array of object in javascript? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 months ago.
const data = [{
"hello":'thameem',
"age":24
},
{
"hello":'thameem',
"age":25
}];
console.log(data);
I need all age values
let data = [{ "hello":'thameem', "age":24 }, { "hello":'thameem', "age":25 }];
// will give you an array to ages
data = data?.map(item => item.age)
Use map to create an array
const ages = data.map((d) => d.age);
console.log(ages);
If you don't need new array and just want to access the property, you may use forEach
data.forEach((d) => {
// do something with your data
}

How change the name of property in array of objects good way? [duplicate]

This question already has answers here:
Changing the key name in an array of objects?
(11 answers)
Rename Object Key in an array using javascript
(6 answers)
How to rename key of object in an array
(4 answers)
Closed 1 year ago.
Now I have object like this
let arr = [{name:'first'},{name:'first1'}]
Exprected
[{value:'first'},{value:'first1'}]
My attempt
let result = arr.map(el=>{
const {name, ...other} = el;
return {value: el.name, ...other}
})
you can do it that way!
let arr = [{name:'first'},{name:'first1'}] //{value: 'first'}
let result = arr.reduce((acc,el)=>{
return [...acc, {'value' : el.name}]
},[])
You can transform the data using Array.prototype.map.
let arr = [{name:'first'},{name:'first1'}]
let result = arr.map(x => {return {value: x.name}});
console.log(result);

Convert array of arrays, and returns an object with each pair of elements in the array as a key-value pair [duplicate]

This question already has answers here:
Reversing an Object.entries conversion
(7 answers)
How to create an object from an Array of key-value pairs?
(7 answers)
Closed 2 years ago.
Input:
var array = [["make", "BMW"], ["model","X6"], ["year",2020]];
My Output should be like this:
var object = {
make : “BMW”
model : “X6”,
year : 2020
}
const array = [["make", "BMW"], ["model","X6"], ["year",2020]];
let obj={}
for(let item of array){
obj[item[0]]=item[1]
}
console.log(obj)
You can use Object.fromEntries()
const arr = [["make", "BMW"], ["model","X6"], ["year",2020]];
const res = Object.fromEntries(arr);
console.log(res)

In javascript, how to convert string array into array of object? [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 4 years ago.
I have the following array of string that needs to be turn into array of object with the keys value and label
languages: ["Arabic", "Irish"]
0:"Arabic"
1:"Irish"
length: 2
If I wanted to make this array into an array of object like the following:
languages = [
{ value: 'Arabic', label: 'Arabic' }
{ value: 'Irish', label: 'Irish' }
]
How will I do that? Can someone please help?
You can use Array.map():
var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);
You can also use Array.forEach() as:
var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);

How to convert an object of key values to an array of objects [duplicate]

This question already has answers here:
How to transpose a javascript object into a key/value array
(11 answers)
Closed 5 years ago.
I want to convert an object of key values to an array of objects in javascript
var obj={"name1":"value1","name2":"value2",...};
How can i convert it to
arr=[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"},...];
Try with array#map and Array#push
var obj={"name1":"value1","name2":"value2"};
var res=[];
Object.keys(obj).map(a => res.push({name:a , value:obj[a]}))
console.log(res)
Short answer (ES6):
const arr = Object.entries(obj).map(([name, value]) => {
return {
name,
value
}
});
Another answer (ES5):
var arr = Object.keys(obj).map(function(key) {
return {
name: key,
value: obj[key]
}
});

Categories