How can I get access to multiple values of nested JSON object?

peace_love

I try to access to my data json file:

[{"id":1,"name":"Maria","project":[{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}]}

This is my approach:

data[0].name;

But like this I get only the result:

Animals

But I would need the result:

Animals, Cats
Feathercrown

If that's your actual data object, then data[0].name would give you "Maria". If I'm reading this right, though, you want to get all the names from the project array. You can use Array.map to do it fairly easily. Note the use of an ES6 arrow function to quickly and easily take in the object and return its name.

var bigObject = [{"id":1,"name":"Maria","project":[{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}]}];
var smallObject = [{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}];

console.log("Getting the names from the full array/data structure: "+bigObject[0].project.map(obj => obj.name))
console.log("Getting the names from just the project array: "+smallObject.map(obj => obj.name))

EDIT: As per your comment on the other answer, you said you needed to use the solution in this function:

"render": function (data, type, row) {if(Array.isArray(data)){return data.name;}}

To achieve this, it looks like you should use my bottom solution of the first snippet like so:

var data = [{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}];

function render(data, type, row){
  if(Array.isArray(data)){
    return data.map(obj => obj.name);
  }
};

console.log("Render returns \""+render(data)+"\" as an array.");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i get an object with checks on nested object in Spring boot?

How can I access the properties of a nested object in Angular?

How can I access JSON property of nested object inside object array

How can I access these json object in python

How can i convert a plain JSON object to nested JSON Object

How to get values from nested JSON object using jquery?

How can I access this json response object?

How to get values of Nested Object (JSON) to pass it as Props in ReactJS

how to get multiple values from JSON object

how do i access deeply nested json object in angularjs

How can I get multiple/nested class return values from deserializing json.net in unity?

How do I get the json nested object?

How can I access my JSON object?

How can I access values in this JSON?

How can I access the property of a nested object in Javascript?

Access object values in array nested in JSON object

How can I access the position of an object in the array (json) and change its values by php

How can i get nested json objects values in single key value pair array

How to get array of checked Values from the nested JSON Object

How can I get multiple values?

How can I access a nested object inside an array to validate it?

How can I get value from object with nested object?

How do I get nested json values from parsed JSON?

How can I get multiple unique values from a JSON with JQuery?

How can I access deeply nested and unreliable json values in a simple and expressive way like kotlin?

Jmeter - How to get nested object in json with multiple object

How can I access nested object and render it to datatable?

How, with Ruby, can I access and compare these nested array values?

How can I add the correct depth value to a nested json object when children have multiple parents