Create options list for react Select from single dimension array [closed] - javascript

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}])

Related

Change array elements [closed]

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 8 months ago.
Improve this question
There is an array
[ [ 'Alex', '167%' ],
[ 'Benjamin', '127%' ],
[ 'Elijah', '117%' ],
[ 'Liam', '136%' ],
[ 'Theodore', '135%' ],
[ 'Mia', '128%' ] ]
I need to make it look like this
[ 'Alex 167%',
'Benjamin 127%',
'Elijah 117%',
'Liam 136%',
'Theodore 135%',
'Mia 128%' ]
I need the elements of each nested array to be combined into a string and such a number of spaces are inserted between them so that the length of this string is 29 characters
You could use a map combined with padEnd
const items = [
['Alex', '167%'],
['Benjamin', '127%'],
['Elijah', '117%'],
['Liam', '136%'],
['Theodore', '135%'],
['Mia', '128%']
];
const combined = items.map(([name, percent]) =>
name.padEnd(29 - percent.length, ' ') + percent
);
console.log(combined);

Increment array object duplicate keys by 1 in javascript

This is my array
[
{Product Name: "yafuyafu"},
{Parameter: "sss"},
{Description: "uyghik"},
{Parameter: "some"},
{Description: "YogaYoga"}
]
The key Parameter and Description appears twice here and I want it to be like this
[
{Product Name: "yafuyafu"},
{Parameter: "sss"},
{Description: "uyghik"},
{Parameter1: "some"},
{Description1: "YogaYoga"},
{Parameter2: "some"},
{Description2: "YogaYoga"}
{Parameter3: "some"},
{Description3: "YogaYoga"}
]
I populate this Select with the Keys of my array objects
This select would not add the duplicate Parameter and description keys and i believe it is because of the name and besides that i want the user to be able to differentiate them.
Please can someone help me figure this out, thank you very much in advance.

How to restructure objects of data into arrays of objects? [closed]

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);

How to delete particular elements in a array of objects [closed]

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 4 years ago.
Improve this question
I have an array with multiple objects.How can i delete an element in a
object.
For example i have attached a image in that i need to delete
organization element.
Please help me out to fix.
var a = {name: 'pardeep', 'sirname': 'jain'}
delete a['name']
console.log(a);
Just use delete keyword for the key like this -
delete objectName['keyName']
Use the delete operator
const deleteAttribute = (array, attribute) => array.map(x => {
// Delete the attribute here
delete x[attribute];
return x;
});
const arr = [{
attr: 'hello',
color: 'red',
organization: 'Good'
}, {
attr: 'world',
color: 'blue',
organization: 'bad'
}];
console.log(deleteAttribute(arr, 'organization'));
You can use map and delete like below
var arr = [{
name: 'hi',
test: 'x',
organization: 'X'
}, {
name: 'guys',
test: 'y',
organization: 'Y'
}];
console.log(arr.map(x => { delete x.organization; return x}));

How to merge 2 arrays with similar objects? [closed]

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 4 years ago.
Improve this question
IE: I have 2 arrays one with prices, one with names.
The one with prices is longer, and I only want the final array to be the size of the smaller array with only names.
Objects in the prices array:
{
currency: 'BTC',
price: '6500'
},
{
currency: 'NEM',
price: '1'
},
Objects in the name array:
{
currency: 'BTC',
name: 'Bitcoin'
}
The final array should only contain objects that exist in the name array, but also have the price key from the prices array.
{
currency: 'BTC',
name: 'Bitcoin',
price: '6500'
}
I had accomplished this using an NPM package, however the package is old and there is a bug when compiling:
Error while running NPM run build (ERROR in index_bundle.js from UglifyJs)
I also found this answer here: How to merge 2 arrays with objects in one? However none of the answers worked. Neither was the array filtered by the smaller array, but the keys were not combined either.
An alternative is using the function map to generate a new array with the desired output.
This approach uses the function find to retrieve the specific object price related to an object name name.currency === price.currency.
let prices = [{ currency: 'BTC', price: '6500'},{ currency: 'BSS', price: '850'},{ currency: 'USD', price: '905'}],
names = [{ currency: 'BTC', name: 'Bitcoin'},{ currency: 'BSS', name: 'Bolivar'}],
result = names.map(n => Object.assign({}, n, prices.find(p => p.currency === n.currency)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Categories

Resources