0

Below is my object inside array. I need to display the list reading this array.

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};

I need to read and display details.code which does not work. I need to display list of all code

123
456
789

I do not need individual to display. Not like details[0].code. Below is what i have done

let det = [];
Object.keys(res.details).forEach((code) => {            
     det.push(res.details[code])
});

Here det does not give list of code. How to achieve this?

3
  • res.details.forEach(({ code }) => console.log(code)) Commented Apr 18, 2018 at 5:31
  • res.details is an array use for loop res.details.forEach(function(element) { console.log(element); }); Commented Apr 18, 2018 at 5:31
  • try det.push(res.details[code.code]) Commented Apr 18, 2018 at 5:32

7 Answers 7

2

Use map function. Its more appropriate than having separate array and using forEach push items into it.

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};

const mapped = res.details.map(item => item.code);

console.log(mapped);

You can also destruct the object in the parameters list

const mapped = res.details.map(({ code }) => code);
Sign up to request clarification or add additional context in comments.

Comments

1

try this

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};
var data = res.details.map(function(item) { return item["code"]; });

console.log(data);
    

Comments

1

You may not require Object.keys here.Use array map method , it will return an array of code value

let res = {
  details: [{
      "code": "123",
      "name": "tye"
    },
    {
      "code": "456",
      "name": "San Joaquin"
    },
    {
      "code": "789",
      "name": "Stanislaus"
    },
  ]
};
let det = res.details.map((code) => {
  return code.code
});
console.log(det)

Comments

1

Try map() which is more suitable in your case:

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};


let codeArr  = res.details.map(c => c.code);

console.log(codeArr);

If you want to use forEach() just ignore Object.keys:

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};

let det = [];
res.details.forEach(code => det.push(code.code));
console.log(det)

Comments

1

 
 $(document).ready(function(){
 var details = [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]

for(var i=0;i< details.length;i++)
{
   var markup='<li>'+ details[i].code+'</li>';
    $('ul').append(markup);
}
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<ul>
</ul>
</body>

Comments

1

let res = {      
  details: [
        {
            "code":"123",
            "name":"tye"
        },
        {
            "code":"456",
            "name":"San Joaquin"
        },
        {
            "code":"789",
            "name":"Stanislaus"
        },
    ]
};

var arr = [];
for (var key in res.details) {
    var obj = res.details[key].code;
    arr.push(obj);
}
    console.log(arr);

Comments

0

I do not understand why people are suggesting answers with map. Conventionally, we use map when we are actually modifying the array of object to some other structure. You can use forEach to achieve this followed by destructuring assignment inside forEach like {code} in the forEach function that will get you the value of the code property of each object in the res.details array:

let res = {
  details: [
    {
      "code":"123",
      "name":"tye"
    },
    {
      "code":"456",
      "name":"San Joaquin"
    },
    {
      "code":"789",
      "name":"Stanislaus"
    },
  ]
};
var codeArray = [];
res.details.forEach(({code}) => codeArray.push(code));

console.log(codeArray);

3 Comments

You you are modifying it to some other structure?! You're mapping an object to a string. Your answer is exactly OPPOSITE of what you're supposed to do. map is correct.
@Li357 please comment after investigating the code and output
I know what the code is supposed to do and what yours does. If you have to forEach and push to another array, then you can map which is preferred.

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.