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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644512a4637889.html
评论列表(0条)