[英]Javascript: creating new column in array of objects
I have an array of objects like so:我有一个像这样的对象数组:
var arr1 = [
{"Date": "2017-04-15", "Price":"300"},
{"Date": "2017-04-16", "Price":"310"},
{"Date": "2017-04-17", "Price":"320"},
]
I wish to add a new key( newDate
)-value(JS Date
object) pair in each row.我希望在每一行中添加一个新的键( newDate
)-值(JS Date
对象)对。 That is, I want to end up with:也就是说,我想结束:
[
{"newDate":2017-04-14T14:00:00.000Z, "Date": "2017-04-15", "Price":"300"},
{"newDate":2017-04-15T14:00:00.000Z, "Date": "2017-04-16", "Price":"310"},
{"newDate":2017-04-16T14:00:00.000Z, "Date": "2017-04-17", "Price":"320"},
]
My script so far, which I run in node
:到目前为止,我在node
运行的脚本:
function getDateObject(dateString) {
// Splits a yyyy-mm-dd string, and returns a Date object
var parts = dateString.split("-")
var myDate = new Date(parts[0], parts[1]-1, parts[2])
return myDate
for (var i = 0; i < arr1.length; i++) {
console.log(arr1[i])
arr1[i]["newDate"] = getDateObject(arr1[i]["Date"])
}
This gives an error:这给出了一个错误:
TypeError: Cannot read property 'split' of undefined at getDateObject
That is, it's throwing at error at the line: var parts = dateString.split("-")
.也就是说,它在以下行抛出错误: var parts = dateString.split("-")
。 I'm puzzled by this syntax error because of course dateString
is not defined;我对这个语法错误感到困惑,因为dateString
没有定义; it's meant to be an input parameter, rather than a variable instantiated by var
, let
or const
or something.它意味着是一个输入参数,而不是一个由var
、 let
或const
或其他东西实例化的变量。 Am I missing something?我错过了什么吗?
Bonus: would be great if someone could suggest a way to do this with array.map
.奖励:如果有人可以建议一种使用array.map
执行此操作的方法, array.map
。
Another solution using Array.map()
and destructuring
could be next one:使用Array.map()
和destructuring
另一种解决方案可能是下一个:
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"}, ]; console.log( arr1.map(o => ({newDate: new Date(o.Date), ...o})) );
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
Or use Array.forEach()
if you don't mind to change the original array
:或者如果您不介意更改原始array
请使用Array.forEach()
:
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"}, ]; arr1.forEach(o => o.newDate = new Date(o.Date)); console.log(arr1);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
In your code arr[i]
is has no key 0
.在您的代码中arr[i]
没有键0
。 If you want to access the Date
use Property Accessors arr[i].Date
or you can use arr[i]['Date']
如果你想访问Date
使用属性访问器arr[i].Date
或者你可以使用arr[i]['Date']
A better way would be to use map()
and Object.assign()
更好的方法是使用map()
和Object.assign()
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"}, ] arr1 = arr1.map(obj => (Object.assign(obj,{newDate:new Date(obj.Date)}))); console.log(arr1);
arr1
is an array of objects, not an array of arrays. arr1
是一个对象数组,而不是数组数组。 Referencing arr1[i][0]
only makes sense if you're trying to get something from the 0th property on the object being iterated over, but no such property exists, thus undefined
.引用arr1[i][0]
仅当您尝试从正在迭代的对象的第 0 个属性中获取某些内容时才有意义,但不存在这样的属性,因此undefined
。 Pass arr[i].date
instead:通过arr[i].date
代替:
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"} ] function getDateObject(dateString) { // Splits a yyyy-mm-dd string, and returns a Date object var parts = dateString.split("-") var myDate = new Date(parts[0], parts[1]-1, parts[2]) return myDate } for (var i = 0; i < arr1.length; i++) { arr1[i]["newDate"] = getDateObject(arr1[i].Date) } console.log(arr1);
Or, with .map
:或者,使用.map
:
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"} ]; const arr2 = arr1.map((obj) => { const [y, m, d] = obj.Date.split('-'); return { ...obj, newDate: new Date(y, m - 1, d) }; }); console.log(arr2);
I think you need to access "Date"
instead of 0
in your for-loop我认为您需要在 for 循环中访问"Date"
而不是0
arr1[i]["newDate"] = getDateObject(arr1[i]["Date"])
// or
arr1[i].newDate = getDateObject(arr1[i].Date)
Since your Date
property of the array object in valid JS date format you can simply use new Date()
function of JS to create newDate
property following sinpet is simplest pure JS code由于您的数组对象的Date
属性是有效的 JS 日期格式,因此您可以简单地使用 JS 的new Date()
函数来创建newDate
属性,下面的 sinpet 是最简单的纯 JS 代码
var arr1 = [ {"Date": "2017-04-15", "Price":"300"}, {"Date": "2017-04-16", "Price":"310"}, {"Date": "2017-04-17", "Price":"320"} ] //adding new date for(var i = 0; i < arr1.length; i++) arr1[i].newDate = new Date(arr1[i].Date); //test console.log(arr1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.