This question already has answers here:
Why are Objects not Iterable in JavaScript?
(7 answers)
Closed 2 months ago.
why Objects are not iterable? bcoz they are , we can iterate with for in loop and why to use 'iterable objects' and push Symbol.iterator in Object?!
You may use the Object.keys to iterate the object. The Object.keys returns an iterable array containing the keys of the object. See example:
var obj = { a:1, b:2, c:100, d:120};
var objItems = Object.keys(obj);
objItems.forEach( (item, i)=>{
console.log(obj[item]);
});
Related
This question already has answers here:
JavaScript flattening an array of arrays of objects
(14 answers)
Closed 4 months ago.
I'm working with an API and after I try to clean up the data, I got an array of arrays of arrays:
arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]]
How can I clean this up to something like this:
arr = [{name: "john"},{name: "jack"},{name: "joe"},{name: "bob"}]
You can use Array.prototype.flat(), providing Infinity as the depth argument to flatten all sub-arrays recursively:
const arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]];
const flattened = arr.flat(Infinity);
console.log(flattened);
In this case calling arr.flat().flat() would do the trick.
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
This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 6 years ago.
I have two JavaScript objects like
let master = {A:0, B:2};
let slave = {B:1, C:1};
I need:
result == {A:0, B:2, C:1};
Does JS have a simple command to merge in that way?
Use Object.assign
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
let master = {A:0, B:2};
let slave = {B:1, C:1};
console.log(Object.assign(slave, master));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to sort an array of javascript objects?
i have a array of objects:
var arr = []
arr[0] = new Object()
...
arr[n] = new Object()
I want to get sorted array by arr[i].getSortOrder() for example, where arr[i].getSortOrder() return integer value. How to do it ?
Just for the record:
arr.sort(function(a, b){return a.getSortOrder() - b.getSortOrder();});
For details see Sorting an array of JavaScript objects and the MDN docs for the sort() method.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting a JavaScript object
Sort JavaScript object by key
I have array:
var arr = {}
arr[2323] = 1
arr[123] = 1
...
arr[n+232323] = 1
How to get all element of aobject sorted by key ( number order ? )
for ( key in arr ) {
alert(typeof(key))
}
return string type.
This is not an assosiative array, this is an object. There are no associative arrays in javascript.
Additionally, objects are not ordered. The order of keys in an object is meaningless.
Assuming there's some reason you don't use an Array in the first place, you can get an Array of the enumerable object properties, and sort that Array...
var sorted = Object.keys(my_obj)
.sort(function(a,b) {
return a - b;
});
This assumes the keys are numeric.
Then you can iterate the Array, and use each key to get the value from my_obj...
sorted.forEach(function(key) {
console.log(my_obj[key]);
});
Short answer: You can't.
Long answer: Associative Arrays in JavaScript are really JavaScript objects. When you add a new element, you're really adding a new member to the object. While most browsers will enumerate those members in the order they were added, the standard states that the order is undefined. You can't sort something that is undefined.
JavaScript objects (maps/dictionaries/associative arrays) have no order, you can't sort them. You will need to convert it to an array first. As you only need the keys of your object in your loop, the Object.keys() function (potentionally needs a shim for older browsers) is destined for the task:
var obj = {...};
var keys = Object.keys(obj).sort(function(a,b){return a-b;}); // numerically sorted
for (var i=0; i<keys.length; i++) {
alert(keys[i]);
// access the values by obj[keys[i]]
}