转换javascript对象列表

时间:2018-11-18 13:57:26

标签: javascript arrays json object javascript-objects

我需要转换这些数据:

var data = [
 {a : "fin", year: 1996, value: 1},
 {a : "fin", year: 1997, value: 2},
 {a : "fin", year: 1998, value: 3},
 {a : "fin", year: 1999, value: 4},
 {a : "swe", year: 1996, value: 5},
 {a : "swe", year: 1997, value: 6},
 {a : "swe", year: 1998, value: 7},
 {a : "swe", year: 1999, value: 8}
]

对此:

var data = [
 {a : "fin", years: [1996,1997,1998,1999], values: [1,2,3,4]},
 {a : "swe", years: [1996,1997,1998,1999], values: [5,6,7,8]}
]

谢谢您的帮助!

3 个答案:

答案 0 :(得分:2)

使用 .filter()方法,我们首先检查相关国家/地区是否已在“结果”数组中包含对象,如果没有,则推送格式化的对象(如果存在)我们已经存在,我们可以使用 .find()方法找到对象,并将年份和值推入对象中的年份和值数组。

let result = [];

data.forEach(element => {
    if (result.filter(obj => obj.a === element.a).length < 1) {
        result.push({a : element.a, years: [element.year], values: [element.value]})
    } else {
        result.find(country => country.a === element.a).years.push(element.year)
        result.find(country => country.a === element.a).values.push(element.value)
    }
})

console.log(result);

答案 1 :(得分:0)

不是很漂亮,但是您会明白的:

var data = [
 {a : "fin", year: 1996, value: 1},
 {a : "fin", year: 1997, value: 2},
 {a : "fin", year: 1998, value: 3},
 {a : "fin", year: 1999, value: 4},
 {a : "swe", year: 1996, value: 5},
 {a : "swe", year: 1997, value: 6},
 {a : "swe", year: 1998, value: 7},
 {a : "swe", year: 1999, value: 8}
]

var results = [];
data.forEach((item, i) => {
  let exitingIndex = getPosition(item.a);

  if (exitingIndex !== undefined) {
    results[exitingIndex].year.push(item.year)
    results[exitingIndex].value.push(item.value)
  } else {
    results.push({
      a: item.a,
      year: [item.year],
      value: [item.value]
    });
  }
})

function getPosition(value) {
  let res;
  results.forEach((item, index) => {
      if (item.a === value) {
        res = index;
        return;
      }
  })

  return res;
}

console.log(results);

答案 2 :(得分:0)

检查是否可行。

    let data = [
    { a: "fin", year: 1996, value: 1 },
    { a: "fin", year: 1997, value: 2 },
    { a: "fin", year: 1998, value: 3 },
    { a: "fin", year: 1999, value: 4 },
    { a: "swe", year: 1996, value: 5 },
    { a: "swe", year: 1997, value: 6 },
    { a: "swe", year: 1998, value: 7 },
    { a: "swe", year: 1999, value: 8 }
]
let groupByCountries = {};
data.forEach(country => {
    if (!groupByCountries.hasOwnProperty(country.a)) {
        groupByCountries[country.a] = {
            years: [country.year],
            values: [country.value]
        }
    }
    else {
        groupByCountries[country.a].years.push(country.year);
        groupByCountries[country.a].values.push(country.value);
    }
})

let combinedValues = []
Object.keys(groupByCountries).forEach(country => {
    combinedValues.push({
        a: country,
        years: groupByCountries[country].years,
        values: groupByCountries[country].values
    })
})

//just for printing
combinedValues.forEach((country) => {
    console.log(country)
})



let data = [
    { a: "fin", year: 1996, value: 1 },
    { a: "fin", year: 1997, value: 2 },
    { a: "fin", year: 1998, value: 3 },
    { a: "fin", year: 1999, value: 4 },
    { a: "swe", year: 1996, value: 5 },
    { a: "swe", year: 1997, value: 6 },
    { a: "swe", year: 1998, value: 7 },
    { a: "swe", year: 1999, value: 8 }
]
let groupByCountries = {};
data.forEach(country => {
    if (!groupByCountries.hasOwnProperty(country.a)) {
        groupByCountries[country.a] = {
            years: [country.year],
            values: [country.value]
        }
    }
    else {
        groupByCountries[country.a].years.push(country.year);
        groupByCountries[country.a].values.push(country.value);
    }
})

let combinedValues = []
Object.keys(groupByCountries).forEach(country => {
    combinedValues.push({
        a: country,
        years: groupByCountries[country].years,
        values: groupByCountries[country].values
    })
})

//just for printing
combinedValues.forEach((country) => {
    console.log(country)
})