0

I have a nested array in json as followed:

var json.result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},    
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},        
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"}]

And this is what I am trying to do:
1/ Find every "name" in the array.
2/ Print the values of "id" and "category" that belongs to the "name" accordingly

Ideal result:
equipment id = 0
category: Camera
name: 600D Kit

equipment id = 1
category: Camera
name: 600D Kit

equipment id = 2
category: Camera
name: 7D Kit

This is my attempt:

var equipmentName = [];
var listLength = json.result.length;
while (listLength--){
    equipmentName[listLength] = json.result[listLength].name
}
console.log(equipmentName);

var equipment = json.result.find(function(e){           
    return e.name == equipmentName;             
});
console.log("equipment id: " + equipment.id);
console.log("category: " +equipment.category);                      
console.log("name: " +equipment.name);

I am able to print all the values of "names" within the array, and print the "id", "category" and "name" of the first item of the array if return e.name;. However, once I added return e.name == equipmentName; , I got an error of "equipment

1
  • As per your ideal result, you just want to iterate over the array and paint the values. Is my understanding correct or there is something more to it? Commented Dec 5, 2015 at 14:17

1 Answer 1

1

This code will display all your elements to the console.

var result= [
 {"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},    
 {"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},        
 {"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"}
];

result.map(function(element){  
  console.log(element.id,element.category,element.name);  
  // Element display functions here
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.