0

I need to transform this data:

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}
]

into this:

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]}
]

Thank you for your help!

3 Answers 3

2

Using .filter() method we first check to see if the country in question already has an object in the 'result' array, if it doesn't we push an object formatted, if it already exists we find the object using .find() method and push the year and value to the years and values arrays within the object.

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I have multiple countries in the real problem.
@vinkki I have updated the solution to work with any number of countries dynamically adding the objects to the result array.
0

Is not that pretty but you'll get the idea:

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 Comments

This works, thanks a lot for your help. If you find a better solution let me know. :)
With lodash it should be one line, but I don’t know if in your case it worth to add a library just for that.
0

Check whether this works.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.