How to map more than one property from an array of objects
You can use .map()
with Object Destructuring:
let data = [ {a:1,b:5,c:9}, {a:2,b:6,c:10}, {a:3,b:7,c:11}, {a:4,b:8,c:12}]; let result = data.map(({ a, b }) => ({a, b}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
map multiple properties in array of objects to the same array
You can map over the objArray
to get the values, but then you end up with a nested array like so: [[1,2],[3,4],[5,6]]
. Use apply
to pass the nested arrays as attributes to concat
to concatenate into a single array.
var objArray = [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6, c: 7}];var reducedArray = [].concat.apply([], objArray.map(Object.values));console.log(reducedArray);
How to reduce an array of objects with two properties into two arrays, one for each property?
You can do
const canais = channel.map(item => item.canais)
const topicos = channel.map(item => item.topicos)
Map array by nested array object property, to an array of strings
You can use flatMap
and map
const arr = [{name:"aaa",inputs:[{inputName:"input-1",groups:[{groupName:"group-a"}]}]},{name:"bbb",inputs:[{inputName:"input-2",groups:[{groupName:"group-b"}]}]}];
const res = arr.flatMap(
o => o.inputs.flatMap(
o => o.groups.map(o => o.groupName)
)
);
console.log(res);
Create an array of objects with multiple properties using javascript
Use map
var output = name.map( (s, i) => ({name : s, shade : shade[i]}) );
Demo
var name1 = ["black","white","red","blue","yellow"];
var shade = ["dark","light","dark","dark","light"];
var output = name1.map( (s, i) => ({name : s, shade : shade[i]}) );
console.log( output );
How to move object properties inside an array of objects
let currentArray = [{
year: 2011,
asset: {
silver: 322,
gold: 325,
},
},
{
year: 2012,
asset: {
silver: 411,
gold: 2235,
},
},
];
currentArray.map(item => {
Object.assign(item, item.asset)
delete item.asset;
return item
})
console.log(currentArray)
From an array of objects, extract value of a property as array
Here is a shorter way of achieving it:
let result = objArray.map(a => a.foo);
OR
let result = objArray.map(({ foo }) => foo)
You can also check Array.prototype.map()
.
Map multiple objects into one array
You can easily achieve this with Array.reduce
:
const data = [{FirstUsername: 5}, {SecondUsername: 4}, {ThirdUsername: 3}, {FourthUsername: 2}, {FifthUsername: 1}]
const oneObject = data.reduce((total, current) => {
const keyName = Object.keys(current)[0]
total[keyName] = current[keyName]
return total
}, {})
console.log(oneObject)
Related Topics
How to Set Time Delay in JavaScript
How to Execute Promises Sequentially, Passing the Parameters from an Array
Best Way to Get the Key of a Key/Value JavaScript Object
Merging or Combining Two Onedit Trigger Functions
How to Prevent Form from Submitting Multiple Times from Client Side
Alternative to a Million If Statements
JavaScript How to Parse JSON Array
Relation Between Commonjs, Amd and Requirejs
How to Detect Window.Print() Finish
What's the Cause of the Error 'Getaddrinfo Eai_Again'
How to Find Out Which JavaScript Events Fired
Typeerror: Console.Log(...) Is Not a Function
How to Post Form Data with Fetch API
Unexpected Behavior Using Array Map on an Array Initialized with Array Fill
What Is This JavaScript Pattern Called and Why Is It Used