This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 2 years ago.
I have an object {5:"James",1:"John", 3:"Jade"}. When reading its entries it should read it in below way {1:"john",3:"Jade",5:"James"}, without modifying the actual object.
Just sort the object
const obj = {5:"James",1:"John", 3:"Jade"};
const result = Object.fromEntries( Object.keys(obj).sort().map((key) => {
return [key, obj[key]];
}));
console.log(result);
Related
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 })))
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 3 years ago.
I have this Array Object that looks like this:
let arr = [
{Name: "sub", Value: "ababbnnn"}
]
I'm trying to access the value of the Name custom:network meaning I want to output this: abcdef1233bfgh. So far I have this loop but I wonder if there may be a cleaner way. Thanks a lot in advance. Here's my code:
You can use the find method:
const value = arr.find(item => item.Name === "custom:network").Value
To cover the case where no item is returned by find, you can use the following approach:
const value = (arr.find(item => item.Name === "custom:network") || {}).Value
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}.`);
})
This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 4 years ago.
This how the default api returns. I want to sort them with basis of min_conversion_count properties.
After sorting the structure changes which is affecting my code. How is it possible to sort them with the property and remain the same format.
Code to sort
var rewarddata = res1.response.reward_list;
// console.log(rewarddata);
sortable_data = [];
console.log(rewarddata);
for (var idx in rewarddata)
{
sortable_data.push([idx,rewarddata[idx]]);
sortable_data.sort(function(a, b) {
return a[1].min_conversion_count - b[1].min_conversion_count;
});
console.log(sortable_data);
}
After sorting it changes like this.
You started out with an object, and you put it into an array in order to sort it; once your array is sorted, you have to turn it back into an object.
const sortedObj = sortable_data.reduce((accum, [key, value]) => {
accum[key] = value;
return accum;
}, {});
Note that property ordering is generally not something to rely on. It would be better to save an array of the sorted keys only, and then use that array instead.
This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
How to loop through a plain JavaScript object with the objects as members
(28 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Iterate through object properties
(31 answers)
How to iterate (keys, values) in JavaScript? [duplicate]
(11 answers)
Closed 5 years ago.
Very simple question here, I have the following variable. set up in a format like so {item number: quantity, item number, quantity....}
var x = {4214061251:1,2497227651:2,4214140739:2};
My question is simply how do i loop through this properly to access each element.
I would like to be able to access the item numbers and quantities separately, how can I achieve this?
var x = {4214061251:1,2497227651:2,4214140739:2};
// Array of all keys 4214061251, 2497227651 ...
var keysArr = Object.keys(x);
console.log(keysArr);
// Iterate on keys
keysArr.forEach(
(el) => {
// To access keys
console.log("Key: "+el);
// To access values instead
console.log("Value: "+x[el]);
}
)