在JavaScript中使用数组创建对象

时间:2017-01-28 01:06:27

标签: javascript json object

我正在使用Node并废弃一个网站,然后我将数据作为字符串返回,因此我从字符串中创建一个数组,数组如下所示

我有一个如下所示的数组:

`[ [ '2011\r\n', '24"', '21"' ],
  [ '2010\r\n', '24"', '21"' ],
  [ '2009\r\n', '24"', '21"' ],
  [ '2008\r\n', '24"', '21"' ],
  [ '2007\r\n', '22"', '20"' ],
  [ '2006\r\n', '22"', '20"' ],
  [ '2005\r\n', '22"', '20"' ],
  [ '2004\r\n', '22"', '20"' ],
  [ '2003\r\n', '22"', '19"' ],
  [ '2002\r\n', '22"', '19"' ],
  [ '2001\r\n', '22"', '19"' ],
  [ '2000\r\n', '22"', '19"' ],
  [ '1999\r\n', '22"', '19"' ],
  [ '1998\r\n', '22"', '19"' ],
  [ '1997\r\n', '22"', '19"' ],
  [ '1982\r\n', '18"', '18"' ],
  [ '1981\r\n', '18"', '18"' ],
  [ '1980\r\n', '18"', '18"' ],
  [ '1979\r\n', '18"', '18"' ],
  [ '1978\r\n', '18"', '18"' ],
  [ '1977\r\n', '16"', '16"' ],
  [ '1976\r\n', '16"', '16"' ],
  [ '1975\r\n', '16"', '16"' ],
  [ '1974\r\n', '16"', '16"' ],
  [ '1973\r\n', '16"', '16"' ],
  [ '1967\r\n', '15"', '15"' ],
  [ '1966\r\n', '15"', '15"' ],
  [ '1965\r\n', '15"', '15"' ],
  [ '1964\r\n', '15"', '15"' ] ]
`

我想从这个看起来像这个

的数组中创建一个对象
      var car = [
        {
        "year": 2011, 
        "driver_side": '24"',
        "pass_side": '21"'
        },
        {
        "year": 2010, 
        "driver_side": '24"',
        "pass_side": '21"'
        },
        {
        "year": 2009, 
        "driver_side": '24"',
        "pass_side": '21"'
        },
        {
        "year": 2008, 
        "driver_side": '24"',
        "pass_side": '21"'
        },
        {
        "year": 2007, 
        "driver_side": '22"',
        "pass_side": '20"'
        },

        // And so on...........
  ]

任何帮助?这就是我需要的任何帮助,谢谢你

3 个答案:

答案 0 :(得分:2)

var array = [ [ '2011\r\n', '24"', '21"' ] /* etc. */ ];

var cars = array.map(function (item) {
    return {
        year: parseInt(item[0], 10),
        driver_side: item[1],
        pass_side: item[2]
    };
};

答案 1 :(得分:1)

如果您的阵列被称为a.map(([year,driver_side,pass_side])=>({year, driver_side, pass_side})) ,您只需要:

a.map(([year,driver_side,pass_side])=>({year: +year.trim(), driver_side, pass_side}))

实际上,既然你想要多年成为数字,你必须稍微调整一下:

JSON.parse

如果您以JSON字符串开头,请在字符串上调用select { font: inherit; } 以获取JavaScript数组对象,然后使用上面的表达式。

答案 2 :(得分:1)



const data = [ [ '2011\r\n', '24"', '21"' ],
  [ '2010\r\n', '24"', '21"' ],
  [ '2009\r\n', '24"', '21"' ],
  [ '2008\r\n', '24"', '21"' ],
  [ '2007\r\n', '22"', '20"' ],
  [ '2006\r\n', '22"', '20"' ],
  [ '2005\r\n', '22"', '20"' ],
  [ '2004\r\n', '22"', '20"' ],
  [ '2003\r\n', '22"', '19"' ],
  [ '2002\r\n', '22"', '19"' ],
  [ '2001\r\n', '22"', '19"' ],
  [ '2000\r\n', '22"', '19"' ],
  [ '1999\r\n', '22"', '19"' ],
  [ '1998\r\n', '22"', '19"' ],
  [ '1997\r\n', '22"', '19"' ],
  [ '1982\r\n', '18"', '18"' ],
  [ '1981\r\n', '18"', '18"' ],
  [ '1980\r\n', '18"', '18"' ],
  [ '1979\r\n', '18"', '18"' ],
  [ '1978\r\n', '18"', '18"' ],
  [ '1977\r\n', '16"', '16"' ],
  [ '1976\r\n', '16"', '16"' ],
  [ '1975\r\n', '16"', '16"' ],
  [ '1974\r\n', '16"', '16"' ],
  [ '1973\r\n', '16"', '16"' ],
  [ '1967\r\n', '15"', '15"' ],
  [ '1966\r\n', '15"', '15"' ],
  [ '1965\r\n', '15"', '15"' ],
  [ '1964\r\n', '15"', '15"' ] ]

const cars = []

for (const a of data) {
    cars.push({
        year: Number(a[0]),
        driver_side: a[1],
        pass_side: a[2]
    })
}

console.log(cars)