how to push new data to json object? [duplicate] - javascript

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 1 year ago.
so lets say i have this objects:
{
"8282748274" : "melly",
"2764726482" : "john",
"8268274827" : "carol"
}
how do i add new data to it so it would look like this:
{
"8282748274" : "melly",
"2764726482" : "john",
"8268274827" : "carol",
"0000000000" : "NewDataHere",
"0000000001" : "MoreNewData",
etc
}
i tried using object.push but it didn't work, here's the code i tried to use
let newData = {
"385835638578" : "alex",
};
object.push(newData);
how do i solve this?

Objects do not have a push method. They are not arrays.
In this case, you can use Object.assign:
const obj = {
"8282748274": "melly",
"2764726482": "john",
"8268274827": "carol",
}
let newData = {
"385835638578": "alex",
};
Object.assign(obj, newData)
console.log(obj)

Related

Push an object into an array inside an object [duplicate]

This question already has answers here:
How can I get the full object in Node.js's console.log(), rather than '[Object]'?
(19 answers)
Closed 1 year ago.
First of all, I'm new in node js. I'm trying to create a REST API which looks like this:
[{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [
{
relay_name:"r1",
status:0
},
{
relay_name:"r2",
status:1
}
]
}
}]
But no matter what I'm trying it keeps on showing me below output:
[
{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [Array]
}
}
]
I'm providing you source code that's in below:
var x = [];
var data = {id :1, name:'tushar hasan', email:'mtushar**#gmail.com'};
var relay1 = {relay_name:'r1', status:0};
var relay2 = {relay_name:'r2', status:1};
var devices = {id:2, device_mac: '4A:34:ER:34:12'};
devices.relays = [];
devices.relays.push(relay1);
devices.relays.push(relay2);
data.devices = devices;
x.push(data);
console.log(JSON.stringify(x));
console.log(x);
By the way, when I call JSON.stringify(x), it provides me elements inside the relay array property. But without JSON.stringify(x) it doesn't show the elements inside relay property.
Thanks in advance.
Your code is fine. By default, when converting an object to string (like console.log does), the nested arrays will get converted to the text [Array]). The fact that you see the correct output when you call JSON.stringify asserts that it's really there.

Convert Json Array to single Object in Node JS [duplicate]

This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 1 year ago.
I want to convert JSON array to a single object. PFB the details
Array:
[{ "item-A": "value-1" }, { "item-B": "value-2" }]
Expected Result:
{ "item-A": "value-1", "item-B": "value-2" }
I have tried following options but result is not what I was expecting
let json = { ...array };
json = Object.assign({}, array);
json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});
Result:
{"0":{"item-A":"value-1"},"1":{"item-B":"value-2"}}
You can use Object.assign and spread the array
const arr=[{ "item-A": "value-1" }, { "item-B": "value-2" }];
console.log(Object.assign({},...arr));
You can use reduce like how you did it with more attention like this:
let array = [{ "item-A": "value-1" }, { "item-B": "value-2" }];
let object = array.reduce((prev, curr) => ({ ...prev, ...curr }), {});
console.log(object);

How to attach array into the object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have an object where i am associating nested objects. In few places I want to associate array instead of object. Any help how to achive that.
Ex :
{ "Id": "ID1",{
"Data1" : {
"myData1" : "myVal1"
},
"Data2":[{
"Dat1" : "val1"
},{
"Dat2" : "Val2"
}]
}
}
So in this case somewhere I am attacahing associate by using this code
`ID1[Data1] = "Data1" : {myData1" : "myVal1"}`
Can any body please suggest me how to add array.
You can achieve it as follows:
const obj = {};
obj['prop'] = {
test: 'test'
};
obj['arr'] = [];
obj['arr'].push({
test: 'test'
});
console.log(obj);

How to get the array of the data in JSON - JavaScript [duplicate]

This question already has answers here:
Getting JavaScript object key list
(19 answers)
Closed 3 years ago.
Hello I was trying to get the array of the data which was written in JSON
For example this is my JSON object
{
Data1 : {
Data2 : "hello",
Data3 : "hi"
},
Data4 : {
Data5 : "this is Karan",
}
}
I want the output as an array which contains [Data1, Data4]
Is there any way to do this Thank you
It's simple using Object.keys()
The Object.keys() method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop.
var json = {
Data1 : {
Data2 : "hello",
Data3 : "hi"
},
Data4 : {
Data5 : "this is Karan",
}
}
var keys = Object.keys(json);
console.log(keys);
You can do it in two ways. Either use Object.keys or you can use for..in loop to iterate the object and return push the keys in an array
let data = {
Data1: {
Data2: "hello",
Data3: "hi"
},
Data4: {
Data5: "this is Karan",
}
}
/**** OPTION -1 *****/
let getKeys = Object.keys(data);
console.log('Option-1 Result ', getKeys)
/**** OPTION -2 *****/
let keysArray = [];
for (let keys in data) {
keysArray.push(keys)
};
console.log(keysArray)

Javascript string array to object [duplicate]

This question already has answers here:
Converting string array to Name/Value object in javascript
(4 answers)
Closed 4 years ago.
How do I convert a string array:
var names = [
"Bob",
"Michael",
"Lanny"
];
into an object like this?
var names = [
{name:"Bob"},
{name:"Michael"},
{name:"Lanny"}
];
Super simple Array.prototype.map() job
names.map(name => ({ name }))
That is... map each entry (name) to an object with key "name" and value name.
var names = [
"Bob",
"Michael",
"Lanny"
];
console.info(names.map(name => ({ name })))
Silly me, I forgot the most important part
names.map(name => name === 'Bob' ? 'Saab' : name)
.map(name => ({ name }))
Use the Array.map() function to map the array to objects. The map() function will iterate through the array and return a new array holding the result of executing the function on each element in the original array. Eg:
names = names.map(function(ele){return {"name":ele}});
You can do this too:
var names = [
"Bob",
"Michael",
"Lanny"
];
var objNames = []
names.forEach(name => {
objNames.push({
name
})
})
Using ES6 you can set name and it is equal to name: name
you can use the map function.
In general, list.map(f) will produce a new list where each element at position i is the result of applying f to the element at the same position in the original list.
For example:
names.map(function(s) {
return {name: s}
});

Categories

Resources