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
}
}))
Related
I have an array of objects and within those objects is another object which contains a particular property which I want to get the value from and store in a separate array.
How do I access and store the value from the name property from the data structure below:
pokemon:Object
abilities:Array[2]
0:Object
ability:Object
name:"blaze"
1:Object
ability:Object
name:"solar-power"
How would I return and display the values in the name property as a nice string like
blaze, solar-power ?
I tried doing something like this but I still get an array and I don't want to do a 3rd loop since that is not performant.
let pokemonAbilities = [];
let test = pokemon.abilities.map((poke) =>
Object.fromEntries(
Object.entries(poke).map(([a, b]) => [a, Object.values(b)[0]])
)
);
test.map((t) => pokemonAbilities.push(t.ability));
Sample Data:
"pokemon": {
"abilities": [
{
"ability": {
"name": "friend-guard",
"url": "https://pokeapi.co/api/v2/ability/132/"
},
"ability": {
"name": "Solar-flare",
"url": "https://pokeapi.co/api/v2/ability/132/"
}
}
]
}
Then I am doing a join on the returned array from above to get a formatted string.
It just seems like the multiple map() loops can be optimized but I am unsure how to make it more efficient.
Thank you.
There is no need for a loop within loop. Try this:
const pokemon = {
abilities: [{
ability: {
name: 'friend-guard',
url: 'https://pokeapi.co/api/v2/ability/132/'
},
}, {
ability: {
name: 'Solar-flare',
url: 'https://pokeapi.co/api/v2/ability/132/'
}
}]
};
const pokemonAbilities = pokemon.abilities.map(item => item.ability.name).join(', ');
console.log(pokemonAbilities);
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 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);
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"