Reputation: 2422
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?
Upvotes: 0
Views: 114
Reputation: 4423
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);
Upvotes: 1
Reputation: 11
$(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>
Upvotes: 1
Reputation: 30739
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);
Upvotes: 0
Reputation: 68933
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)
Upvotes: 1
Reputation: 50291
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)
Upvotes: 1
Reputation: 38683
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);
Upvotes: 1
Reputation: 68655
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);
Upvotes: 2