Javascript: Get the subsets of objects from an array of objects [duplicate] - javascript

This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 2 years ago.
I have an array of objects like this:
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
I am trying to get the subset of the array objects like below in Javascript: I also need to add a 10 to value2.
var arrayOne = [{date:'01/01/2017',value2:300}, {date:'02/01/2017',value2:330},{date:'03/01/2017',value2:330}]
Any suggestions would be greatly appreciated.

You could use map to iterate and select
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
console.log(array.map(({ date, value2 }) => ({ date, value2: value2 + 10 })))

Related

Sort an array by a numeric value in each entry [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 1 year ago.
Say I had an array like this:
[{"Platinum":13},{"Bronze":2910},{"Diamond":92},{"Gold":346},{"Silver":649},{"Stone":12561}]
How would I be able to sort them so they organize themselves from largest to smallest by the number:
[{"Stone":12561},{"Bronze":2910},{"Silver":649},{"Gold":346},{"Bronze":2910},{"Diamond":92},{"Platinum":13}]
Any answer is appreciated, I'm completely stuck on this.
let arr = [{"Platinum":13},{"Bronze":2910},{"Diamond":92},{"Gold":346},{"Silver":649},{"Stone":12561}]
arr.sort((a,b) => Object.values(b)[0] - Object.values(a)[0])
console.log(arr)

Merge multiple object values of array into 1 [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 2 years ago.
This is my array:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3'
]
If want to check, if it has similar objects then I want it to be merged into one and return me array as:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
]
How is this possible in JavaScript or TypeScript?
Use Set to remove duplicate values :
array = [ 'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3' ];
var result = [...new Set(array)];
console.log(result);

Convert an array of objects to an array of values from the object [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have an array of country details:
var countryArr = [{"name":"Afghanistan","alpha2Code":"AF"},{"name":"Åland Islands","alpha2Code":"AX"},{"name":"Albania","alpha2Code":"AL"},{"name":"Algeria","alpha2Code":"DZ"},{"name":"American Samoa","alpha2Code":"AS"},{"name":"Andorra","alpha2Code":"AD"},{"name":"Angola","alpha2Code":"AO"}]
How can I get each value of the name attribute from this array?
So I end up with an array of country names:
["Afghanistan", "Åland Islands"...]
This is a job for Array.map!
const justTheNames = countryArr.map(country => country.name);
Explanation: The Array.map method will iterate through the elements of the array, applying the given callback function to each and returning a new array with the return values from the callback. In this case, it'll iterate over the array of country objects and create a new array with just the country names.

How to loop through this javascript and get the (id) and (brandName) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
How to do i get the id value and brandName value
[{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}]
Not sure what you're trying to do as your question is very vague. But if you want to iterate over each element and do something with the values you could do something like this:
let items = [{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}];
items.forEach(elem => {
const {id, brandName} = elem;
console.log(`ID: ${id}. Brand Name: ${brandName}.`);
})

I have my array as arr=[1,2,3,4]. I pushed an element in it arr.push(5). Now I need to display my array as [5,4,3,2,1]. Any Idea? [duplicate]

This question already has answers here:
How can I reverse an array in JavaScript without using libraries?
(36 answers)
Closed 7 years ago.
I have an array as:
var arr = [1,2,3,4]
//Push and element to array
arr.push(5)
//Now, arr = [1,2,3,4,5]
I need to display my array as
Elements in array arr is:
5,1,2,3,4
Arr.reverse() gives me
5,4,3,2,1.
But i need
5,1,2,3,4
Simply use Array.prototype.reverse():
console.log(arr.reverse());
References:
Array.prototype.reverse().
This is the code you need to use :
arr.reverse();
Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp

Categories