Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I have an array like [ 123, 456 ] and it needs to be mapped into a react Select with label and value pairs so the "options" will be:
0: {label: "123", value: 123}
1: {label: "456", value: 456}
I just can't seem to get the map syntax to work for the source array (i.e. 0: 123, 1: 456)
You can write like this:
array.map((item)=> <Option label={item} value={item}/>)
This return an array of holding options with your label and value
If you need the options array you can write this:
let Myarray= [ 123, 456 ];
Myarray.map((item)=> <Option label={item} value={item}/>)
or If you need an array with label and value:
let Myarray= [ 123, 456 ];
let FinalArray=[];
Myarray.map((item)=>[...FinalArray,{label:item,value:item}])
if you want to put " to value
let Myarray= [ 123, 456 ];
let FinalArray=[];
Myarray.map((item)=>[...FinalArray,{label:item.toString(),value:item}])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I currently have 1 array that has 6 names of the category and 6 arrays of objects, each of which includes 5 objects for each category. What's the best way to restructure the 1-category array and 6-arrays of objects so that I can get just 6 arrays of objects contain both data from the array.
let categoryArray = ["a", "b", "c","d","e","f"];
let outcomeArrays = [
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ]
];
What I want is to make an array that has such a structure like below:
[{category: "a",
details: [{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}]},
{category: "b",
details: [{p:11,q:22,r:null},{p:33,q:44,r:null},{p:55,q:66,r:null},{p:77,q:88,r:null},{p:9,q:10,r:null}]},
...
]
I tried to use push and map method. It didn't work. I even tried to use lodash and can't find a method dealing with this situation.
The version from #thoughtsunificator looks right.
This is a variation using Array.map:
let categoryArray = ["a", "b", "c","d","e","f"];
let outcomeArray = [[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],];
const categories = categoryArray.map((cat, index) => ({
category: cat,
details: outcomeArray[index]
}));
console.log(categories);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
My current object is of the form:
[{Vol:'AB', freq:[{used:4786, avail:1319, res:249}]}
,{Vol:'CD', freq:[{used:1101, avail:412, res:674}]}]
I need to change it to the form:
[{Vol:'AB', freq:{used:4786, avail:1319, res:249}}
,{Vol:'CD', freq:{used:1101, avail:412, res:674}}]
How can this be done.
Try
obj.map( s => (s.freq = s.freq[0], s) );
Explanation
Iterate the array using map
Replace each items freq's array with its first item
Demo
var input = [{
Vol: 'AB',
freq: [{
used: 4786,
avail: 1319,
res: 249
}]
}, {
Vol: 'CD',
freq: [{
used: 1101,
avail: 412,
res: 674
}]
}];
input = input.map(s => (s.freq = s.freq[0], s));
console.log(input);