changing the structure of javascript object [closed] - javascript

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

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

Adding an incrementing number to each object in an array of objects with Javascript [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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Say I have an array of objects:
const myArray = [{ color: 'red' }, { color: 'blue'}, { color: 'yellow'}]
How can I expand the object to add a key value pair to each element where the key is number and the value is a number that increments by one in each object? My desired outcome is:
const myNewArray = [{ color: 'red', number: 1 }, { color: 'blue', number: 2 }, { color: 'yellow', number: 3 }]
const myNewArray = myArray.map((item, index) => { return {"number" :index, ...item} } )

How to get highest 3 value from object array - ES6/JS [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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I simply want to get 3 highest price value objects as new array. How would I do that easily ?
For example i got array like this ..
const data =
[ { name: 'x', color: 'red', price: 15 }
, { name: 'y', color: 'black', price: 5 }
, { name: 'z', color: 'yellow', price: 25 }
, { name: 't', color: 'blue', price: 10 }
, { name: 'n', color: 'blue', price: 60 }
]
You can sort the array by the price property (with Array.sort) and get the first three items (with Array.slice):
const arr=[{name:"x",color:"red",price:15},{name:"y",color:"black",price:5},{name:"z",color:"yellow",price:25},{name:"t",color:"blue",price:10},{name:"n",color:"blue",price:60}];
const result = arr.sort((a,b) => b.price - a.price).slice(0, 3)
console.log(result)

How to create array out of object entries? [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 need help creating a JavaScript object:
I have three fields from the input form in React state object:
{
calories: "45",
fats: "56",
proteins: "67"
}
I would like to create a new array i.e. nutrientInformation from this data. The output should resemble this:
nutrientInformation: [
{
calories: 45
},
{
fats: 56
},
{
proteins: 67
}
]
const state = { calories: "45", fats: "56", proteins: "67" };
const nutrientInformation = Object.entries(state).map(([key, value]) => ({
[key]: value
}));
console.log(nutrientInformation);

How to filter out properties from array of object without any duplicate key-value? [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 2 years ago.
Improve this question
I have a fetch method which is giving the below responseJson
[ {id: 1, cust_email: "abcd#gmail.com"},
{id: 2, cust_email: "abcd#gmail.com"},
{id: 3, cust_email: "wxyz#gmail.com"}]
And i want to handle this response to get the below format
[ {cust_email: "abcd#gmail.com"},
{cust_email: "wxyz#gmail.com"}]
Any tips or suggestions ? Thanks
You can try using Set() and map():
var response = [ {id: 1, cust_email: "abcd#gmail.com"},
{id: 2, cust_email: "abcd#gmail.com"},
{id: 3, cust_email: "wxyz#gmail.com"}]
response = [...new Set(response.map(i => i.cust_email))].map(i => ({cust_email: i}));
console.log(response);

Categories

Resources