admin管理员组文章数量:1026373
I'm trying to make this json
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
print the values as follows
['04/24/2018', '04/25/2018']
But I could not do it, any suggestions?
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
for(var key in datesBooking){
console.log(datesBooking[key])
}
I'm trying to make this json
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
print the values as follows
['04/24/2018', '04/25/2018']
But I could not do it, any suggestions?
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
for(var key in datesBooking){
console.log(datesBooking[key])
}
Share
Improve this question
asked Apr 19, 2018 at 11:29
Juan DavidJuan David
2016 silver badges17 bronze badges
1
-
datesBooking.map(x => x.date)
– Satpal Commented Apr 19, 2018 at 11:30
2 Answers
Reset to default 4You could use map
method by passing a provided callback function.
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
console.log(datesBooking.map(function(item){
return item.date;
}));
Another approach is simply using arrow
functions.
datesBooking.map(a => a.date)
If you are explicitly looking for the foreach
method:
var datesBooking = [
{"date": "04\/24\/2018"},
{"date": "04\/25\/2018"}
];
datesBooking.forEach(function(data, index) {
datesBooking[index] = data.date;
});
console.log(datesBooking);
The above code will rewrite over the existing array.
The same can be achieved using map
var datesBooking = [
{"date": "04\/24\/2018"},
{"date": "04\/25\/2018"}
];
datesBooking = datesBooking.map(x => x.date);
console.log(datesBooking);
As you can see, the map
was introduced to simplify such a use of foreach
.
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
I'm trying to make this json
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
print the values as follows
['04/24/2018', '04/25/2018']
But I could not do it, any suggestions?
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
for(var key in datesBooking){
console.log(datesBooking[key])
}
I'm trying to make this json
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
print the values as follows
['04/24/2018', '04/25/2018']
But I could not do it, any suggestions?
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
for(var key in datesBooking){
console.log(datesBooking[key])
}
Share
Improve this question
asked Apr 19, 2018 at 11:29
Juan DavidJuan David
2016 silver badges17 bronze badges
1
-
datesBooking.map(x => x.date)
– Satpal Commented Apr 19, 2018 at 11:30
2 Answers
Reset to default 4You could use map
method by passing a provided callback function.
var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
console.log(datesBooking.map(function(item){
return item.date;
}));
Another approach is simply using arrow
functions.
datesBooking.map(a => a.date)
If you are explicitly looking for the foreach
method:
var datesBooking = [
{"date": "04\/24\/2018"},
{"date": "04\/25\/2018"}
];
datesBooking.forEach(function(data, index) {
datesBooking[index] = data.date;
});
console.log(datesBooking);
The above code will rewrite over the existing array.
The same can be achieved using map
var datesBooking = [
{"date": "04\/24\/2018"},
{"date": "04\/25\/2018"}
];
datesBooking = datesBooking.map(x => x.date);
console.log(datesBooking);
As you can see, the map
was introduced to simplify such a use of foreach
.
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
本文标签: javascriptforeach loop to fetch JSONStack Overflow
版权声明:本文标题:javascript - foreach loop to fetch JSON - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745644510a2160944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论