If your environment supports Object.assign, then you can do the same in a succinct way like this
const arrObj = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}];
console.log(arrObj.reduce(function(result, current) {
return Object.assign(result, current);
}, {}));
// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce(((r, c) => Object.assign(r, c)), {}));
// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));
ES5 solution:
You can use Array.prototype.reduce like this
var resultObject = arrObj.reduce(function(result, currentObject) {
for(var key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
}
}
return result;
}, {});
console.log(resultObject);
# { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
This solution, simply gathers all the keys and their values in every object in the result, which is finally returned to us as the result.
This check
if (currentObject.hasOwnProperty(key)) {
is necessary to make sure that we are not including all the inherited enumerable properties in the result.
Answer from thefourtheye on Stack OverflowIf your environment supports Object.assign, then you can do the same in a succinct way like this
const arrObj = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}];
console.log(arrObj.reduce(function(result, current) {
return Object.assign(result, current);
}, {}));
// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce(((r, c) => Object.assign(r, c)), {}));
// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));
ES5 solution:
You can use Array.prototype.reduce like this
var resultObject = arrObj.reduce(function(result, currentObject) {
for(var key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
}
}
return result;
}, {});
console.log(resultObject);
# { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
This solution, simply gathers all the keys and their values in every object in the result, which is finally returned to us as the result.
This check
if (currentObject.hasOwnProperty(key)) {
is necessary to make sure that we are not including all the inherited enumerable properties in the result.
You could use reduce for an elegant solution:
arrObj.reduce(function(acc, x) {
for (var key in x) acc[key] = x[key];
return acc;
}, {});
See MDN docs for reduce for more information.
JavaScript: generated object with multiple objects inside -> one object - Stack Overflow
JavaScript - Store multiple objects in array and access their properties via - Stack Overflow
node.js - How to convert multiple object to just one object with Javascript? - Stack Overflow
javascript - add multiple objects to object - Stack Overflow
You could split the string by groups and then for value and key.
var string = 'DB3,B0;var DB3,Int2;zahl DB3,Int4;zahl2 DB3,Int6;zahl3 DB3,Int8;zahl4 DB3,Int10;zahl5',
target = Object.assign(
...string.split(' ').map(s => (([v, k]) => ({ [k]: v }))(s.split(';')))
);
console.log(target);
Use Array.reduce
let str = "DB3,B0;var DB3,Int2;zahl DB3,Int4;zahl2 DB3,Int6;zahl3 DB3,Int8;zahl4 DB3,Int10;zahl5";
let obj = str.split(" ").reduce((a,c) => {
let v = c.split(";");
Object.assign(a, {[v[1]]: v[0]});
return a;
}, {});
console.log(obj);
To just add the variables to the array, there's really no other way except to do exactly what you did in your example, which is to manually add each variable to your array.
// Users array
var allUsers = [
user,
jon,
lucy,
mike,
luke,
james,
dave,
sarah,
natalie
];
BUT, you could probably do something a little more robust, which in its simplest form is to define an object literal called users that contains each user object. Then you not only have the added benefit of a pseudo-namespacing around your users, you can easily iterate over the object literal and put each user into an array, as such:
// Users
var users = {
user : {username: user9110252username, genrePref: user9110252genre},
jon : {username: 'Jon', genrePref: 'rock'},
lucy : {username: 'Lucy', genrePref: 'pop'},
mike : {username: 'Mike', genrePref: 'rock'},
luke : {username: 'Luke', genrePref: 'house'},
james : {username: 'James', genrePref: 'house'},
dave : {username: 'Dave', genrePref: 'bass'},
sarah : {username: 'Sarah', genrePref: 'country'},
natalie : {username: 'Natalie', genrePref: 'bass'}
}
// Users array
var allUsers = [];
// Populate users array
for(var key in users) {
allUsers.push(users[key]);
}
One last note on style. You should probably avoid explicitly setting the 'key' of each user object in your users object literal to be the name of the user. If you have any users with the same name, this model will break. Instead, consider generating some kind of unique key, like a guid, or some other unique value for each user. That way you'll never accidentally overwrite an entry in your users object if, for example, you are populating this users object from some database and two or more users happen to have the same name.
Good luck.
var arr = [{username: 'Jon', genrePref: 'rock'},{username: 'Lucy', genrePref: 'pop'},{username: 'Mike', genrePref: 'rock'},{username: 'Luke', genrePref: 'house'},{username: 'James', genrePref: 'house'},{username: 'Dave', genrePref: 'bass'},{username: 'Sarah', genrePref: 'country'},{username: 'Natalie', genrePref: 'bass'}];
var text = "<table border ='1'><tr><th>Username</th><th>genrePref</th></tr>"
for (var i=0;i<arr.length;i++){
text+="<tr><td>"+arr[i].username+"</td><td>"+arr[i].genrePref+"</td></tr>"
}
text+="</table>";
document.write(text);
<html>
<head>
<title>Javascript</title>
</head>
<body>
</body>
</html>
You can map the Object.entries since there is 1:1 of the entries to the array item
const obj = {
"123": { "name": "yourname1", "class": ["art","math"] },
"098": { "name": "yourname2", "class": ["class1","class2"] },
"456": { "name": "yourname3" }
};
const arr = Object.entries(obj)
.map(([key, val]) => ({ "number": key, "name": val.name, "class": val.class || [] }));
console.log(arr)
var obj = {
123: {
name: 'yourname1',
},
'098': {
name: 'yourname2',
},
456: {
name: 'yourname3',
},
};
const obj2 = Object.keys(obj).map(key => ({
number: key,
name: obj[key].name,
}));
console.log(obj2);
The following command may be readable, understandable.
let data = {}
let status = true
let res = [{
"quantity": 82,
"startDate": "2021-05-04T02:09:00Z",
"unit": "bpm"
},
{
"quantity": 79,
"startDate": "2021-05-09T02:09:00Z",
"unit": "bpm"
}
]
data = {res, status} // short command of data = { res: res, status: status}
console.log(data)
Just assign to the properties you want:
data.status = status;
data.res = res;