This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
"order":[
{
"localId" : 2445,
"tariff": {
"tariffName": {
"nameRu": "Добро пожаловать",
"nameEn": "Welcome"
},
........
}
}
]
This is my JSON. I want to access to "nameRu". How to do it inside my React Native app?
Currently, I am able to get "localId" in this way:
{order:localId}
let obj = {order:[
{
localId : 2445,
tariff: {
tariffName: {
nameRu: "Добро пожаловать",
nameEn: "Welcome"
},
}
}
]}
console.log(obj.order[0].tariff.tariffName.nameRu)
I think you're looking for this object destructing. Reference
let d = {
"order": [{
"localId": 2445,
"tariff": {
"tariffName": {
"nameRu": "Добро пожаловать",
"nameEn": "Welcome"
}
}
}]
};
// extract with variable name
let [{
tariff: {
tariffName: {
nameRu
}
}
}] = d.order;
// print
console.log(nameRu);
Related
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
}
}))
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')
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);
This question already has answers here:
Javascript: How to Get Object property using Array of string? [duplicate]
(7 answers)
Closed 3 years ago.
I have a config-file:
{
"permission": {
"users": {
"image": {
"data": "example"
}
}
}
}
And an array with a called path like this:
path = ['users', 'image']
How can I get the data?
First try:
config.permission.path[0].path[1];
Second try:
switch (requestedPath[2]) {
case 'users':
switch (requestedPath[3]) {
case 'image':
mydata = config.permission.users["/image"]
}
}
This will work, but is there a better way?
You need a bracket as property accessor for the object, because you take a variable as key.
config.permission[path[0]][path[1]];
For a more dynamic approach, you could reduce the given data with a default object for not fiund properies.
const getV = (object, path) => path.reduce((result, key) => (result || {})[key], object);
var config = { permission: { users: { image: { data: 'example' } } } },
path = ['users', 'image'];
console.log(getV(config.permission, path));
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"