How to attach array into the object [duplicate] - javascript

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);

Related

Bring data inside object in array along with other attributes [duplicate]

This question already has answers here:
One liner to flatten nested object
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last month.
I have a array as follows:
data = [
{
"data": {
"id":1,
"vol":"0.0"
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0"
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I want to make an arrays from above array such that data inside details object is every element comes outside. So in my final array there will not be details object. I just want to bring all attributes of details object with other object. So my final array will look something like this
data = [
{
"data": {
"id":1,
"vol":"0.0"
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
},
{
"data": {
"id":1,
"vol":"0.0"
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
];
How can I do that?
You can use Array#map with object destructuring.
const arr=[{data:{id:1,vol:"0.0",details:{ABC:"8.30",OFG:"13.85",SPG:"70.80"}}},{data:{id:2,vol:"1.0",details:{ABC:"3.30",OFG:"15.85",SPG:"70.80"}}}];
let res = arr.map(({data: {details, ...rest}}) => ({data : {...rest, ...details}}));
console.log(res);
You could make use of the Spread Operator and do something like this:
const builtData = originalData.map(({ data }) => ({
data: {
id: data.id,
vol: data.vol,
...data.details
}
}))

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

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)

how to get an object inside an array with a value [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
Array looks list :
[
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
]
I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.
You can use find like:
const data = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
];
console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));
Reference:
Array.prototype.find()
result = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
].find(item => item.Uid === '673bjhbjhdcsy')

Nested destructuring [duplicate]

This question already has answers here:
Destructuring deep properties
(4 answers)
Closed 5 years ago.
Say I have an object with a shape like so:
{
rows: [
{some: fields,
go: here}]
}
, and say that, in a particular case, I knew that the length of rows is 1. How could I extract {some: fields, go: here} through destructuring?
I have attempted: {rows: [stuff]}, and {rows: stuff} but in both cases console.log(stuff) prints [{some: fields, go: here}] How can I do this through destructuring?
{rows: [stuff]} works fine:
const obj = {
rows: [
{some: 'fields',
go: 'here'}]
};
const { rows: [stuff] } = obj;
console.log(stuff);

how can I access "Name & Store's Name" from following json [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I have a nested data structure / JSON, how can access a specific value?
{
"List":
[
{"Active":true,"Name":"VMW","Stores":
[
{"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}
]
}
]
}
Its json data, how can I read it using Ajax or Javasricpt
The List and the store is an array, so to retrieve name and store's name, use array index like this :
jsondata.List[0].Name >>> return "VMW"
jsondata.List[0].Stores[0].Name >>> return "Admin"
json = {
"List":
[
{"Active":true,"Name":"VMW","Stores":
[
{"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}
]
}
]
}
json["List"] // { "Active":true,"Name":"VMW","Stores": [{"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}]}
json["List"][0]["Stores"] // {"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}
is Stores for Object
Stores = json["List"][0]["Stores"]
for (i in Stores) (function(active, name) {
console.log(active, name);
}(Stores[i]["Active"], Stores[i]["Name"]));
for results:
true "Admin"
true "sunil"

Categories

Resources