Javascript:在对象数组中创建新列

时间:2019-02-20 04:24:28

标签: javascript

我有一个这样的对象数组:

var arr1 = [
    {"Date": "2017-04-15", "Price":"300"},
    {"Date": "2017-04-16", "Price":"310"},
    {"Date": "2017-04-17", "Price":"320"},
]

我希望在每一行中添加一个新的键(newDate)-值(JS Date对象)对。也就是说,我要结束:

[
    {"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"},
]

到目前为止,我的脚本是在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"])
}

这给出了一个错误:

TypeError: Cannot read property 'split' of undefined at getDateObject

也就是说,它在以下行上出错:var parts = dateString.split("-")。因为没有定义dateString,我对这个语法错误感到困惑。它应作为输入参数,而不是由varletconst实例化的变量。我想念什么吗?

奖金:如果有人可以建议用array.map做到这一点的方法,那就太好了。

5 个答案:

答案 0 :(得分:0)

我认为您需要在循环中访问"Date"而不是0

arr1[i]["newDate"] = getDateObject(arr1[i]["Date"])

// or

arr1[i].newDate = getDateObject(arr1[i].Date)

答案 1 :(得分:-1)

在您的代码arr[i]中没有密钥0。如果您想访问Date,请使用Property Accessors arr[i].Date,也可以使用arr[i]['Date']
更好的方法是使用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);

答案 2 :(得分:-1)

arr1是一个对象数组,而不是数组数组。仅当您尝试从要迭代的对象的第0个属性中获取某些内容时,引用arr1[i][0]才有意义,但是不存在这样的属性,因此undefined。改为通过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);

或者,用.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);

答案 3 :(得分:-1)

另一种使用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})) );

或者如果您不愿意更改原始的Array.forEach(),请使用array

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);

答案 4 :(得分:-1)

由于数组对象的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)