How do I iterate over a dynamic keyed object? [duplicate] - javascript

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 6 years ago.
I have a hashed object where the keys are added dynamically through user selections.
I want to iterate over it and extract the values similar to the way I would do if it was simply an array: selections.map(cart => /*do stuff*/).
How can I achieve this?

Use Object.keys()
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
var array = Object.keys(selections).map(k => selections[k]);
// get all values from the object

Related

I can't iterate over a javascript array [duplicate]

This question already has answers here:
How can JavaScript arrays have non-numeric keys?
(2 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 months ago.
I am going crazy here, I have an associative array as seen below which is defined after the page finishes loading. However Array.forEach is returning undefined and I have no idea why. The array is most definitely populated during the loop. Can anyone give me any ideas? Also doesn't work with JQuery's $.each
Arrays are usually a mapping of index (integer from 0 to 232 − 2, inclusive) to value. In your case you've treated the array as a dictionary e.g. key (string) to value.
You've probably done something like this:
members = new Array();
members['animerox1213'] = 'Ashima';
JavaScript allows this, after all it is still an object:
typeof members === 'object'
But instead of adding a value to the array, you've actually set a non-numeric property on the object called animerox1213. That is not how an array should be used and we can observe this by checking the size:
members.length === 0;
Consequently, forEach does not do anything as it considers it an empty array.
That said, it is enumerable with for…in as it's still just an object (with enumerable properties):
for (m in members) {
console.log(m, members[m]);
}
Consider using just an object e.g. members = {} or Map. Note especially the section Objects vs. Maps.

convert array(2) that has sorted object to object [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Closed 1 year ago.
I'm trying to sort an object from db
MyOjbect : {
key: {
val1:"value",
val2:"",
val3:time,
val4:...
...}
}
and I sorted the object by val3
const sorted = Object.entries(MyObject)
.sort((x,y) => {x[1].val3 - y[1].val3} );
and got an array type.
0:(2) ["key" , {val1:...val2:...}]
1:(2) ["key" , {val1:...val2:...}]
...
I tried reduce() to make it back to the object type, but it changes to be in unsorted order
How can I keep the order sorted and change the type to the object?
You can't. An object doesn't keep the sort order as an Array does it. If you want to have the keys sorted you need to use an Array.

How to merge two arrays at run time [duplicate]

This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.

Access to json variable (node.js) [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
How to parse JSON data when the property name is not known in advance?
(2 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
How can I get the value "success" if the value that stands in place "Everlasting hair" is changing?
{
'Everlasting hair' :
{ succes : true,
volume:'12'
}
}
You can discover the property names in the object via for-in or Object.keys. Object.keys, for instance, will give you an array of the object's own, enumerable properties. If you know for sure there will only be one, then:
var success = yourObject[Object.keys(yourObject)[0]].succes;
// Another s here? It's missing in the question -----------^
On cutting-edge JavaScript engines with Object.values (new in ES2017, but polyfillable), as Keith points out if you don't need the name you can use Object.values instead:
var success = Object.values(yourObject)[0].succes;
// Another s here? It's missing in the question -^

Accessing an Object's properties in Javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hello I am doing a school assignment my main problem is as follows.
var objectQueue = {
customers:[
{name:"Phil", order:"coffee"},
{name:"Sandy", order:"coffee"},
{name:"Enrique", order:"sandwich"},
{name:"Joe", order:"coffee"},
{name:"Alex", order:"muffin"},
{name:"Zoe", order:"chili"},
{name:"Bahamut", order:"sandwich"},
{name:"Rydia", order:"timbits"}
]
};
I have this object, I need to know how to access each customer's order through a for loop. I can't get the loop to read each person's order. What would be the right way to do this?
This is where I am currently:
objectQueue[x]order
Assuming x is a counter:
objectQueue.customers[x].order
objectQueue has a property named customers, to access a simple property on a Javascript object, you can just use its name:
objectQueue.customers
Then, customers has a array of objects. To access elements in a array, we use its index:
customers[0]
Since the elements in the list are maps/objects, we can access them via properties as well:
customers[0].name
Putting this all together we get:
objectQueue.customers[0].name
Almost everything in Javascript is an object, so it's a little misleading to differentiate between arrays and objects (since arrays ARE objects), but I'm assuming you can dig into those details later if you're interested. In the meantime, this should get you going.
You first need to access the length of the customers and use that as your loop count, from there you can use 'i' your counter to access properties
for (i=0; i<objectQueue.customers.length; i++){
console.log(objectQueue.customers[i]);
console.log(objectQueue.customers[i].name);
console.log(objectQueue.customers[i].order);
}

Categories