0

Here is the JSON that I have:

[
    "Spain",
    "2010",
    "Brazil",
    "1994",
    "Brazil",
    "1970",
    "Brazil",
    "1962",
    "Brazil",
    "2002",
    "Brazil",
    "1958",
    "Germany",
    "2014",
    "Germany",
    "1990",
    "Germany",
    "1974",
    "Germany",
    "1954",
    "Italy",
    "2006",
    "Italy",
    "1982",
    "Italy",
    "1938",
    "Italy",
    "1934",
    "France",
    "2018",
    "France",
    "1998",
    "Argentina",
    "1986",
    "Argentina",
    "1978",
    "Uruguay",
    "1930",
    "Uruguay",
    "1950",
    "England",
    "1966"
]

I'm trying to convert it into multiple array objects of key-value pair like below.

{
        "name": "Spain",
        "year": 2010
    },
    {
        "name": "Brazil",
        "year": 1994, 1970, 1962, 2002, 1958
    },
    ...
]

Is there any way to achieve the above structure? I have tried couple of methods using array map function but it doesn't give in the desired output.

const dataMap = Object.keys(s).map((filename) => {
      return {
        name: filename,
        year: s[filename]
    }
    })

How can it be done in Node.js??

1
  • 2
    Shouldn't year property be an array? Commented Sep 5, 2021 at 11:49

3 Answers 3

3

You can easily achieve the result using Map

const arr=["Spain","2010","Brazil","1994","Brazil","1970","Brazil","1962","Brazil","2002","Brazil","1958","Germany","2014","Germany","1990","Germany","1974","Germany","1954","Italy","2006","Italy","1982","Italy","1938","Italy","1934","France","2018","France","1998","Argentina","1986","Argentina","1978","Uruguay","1930","Uruguay","1950","England","1966"];

const map = new Map();
for (let i = 0; i < arr.length; i += 2) {
  const country = arr[i],
    year = arr[i + 1];

  if (map.has(country)) map.get(country).push(year);
  else map.set(country, [year]);
}

const result = [];
for (let [name, year] of map.entries()) {
  result.push({ name, year });
}
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

You can create a Map by looping over your array 2 indexes at a time. For each iteration, you can obtain the place + the year, and create a key-value pair using the place as the key and an array of years as the value. If you come across a place that you've already seen, you can add it to the array at your key in the Map. Lastly, you can use Array.from() with a mapping function to build your array of objects from the Map:

const arr = ["Spain","2010","Brazil","1994","Brazil","1970","Brazil","1962","Brazil","2002","Brazil","1958","Germany","2014","Germany","1990","Germany","1974","Germany","1954","Italy","2006","Italy","1982","Italy","1938","Italy","1934","France","2018","France","1998","Argentina","1986","Argentina","1978","Uruguay","1930","Uruguay","1950","England","1966"];

const map = new Map;
for(let i = 0; i < arr.length; i+=2) {
  const name = arr[i];
  const year = arr[i+1];
  map.set(name, (map.get(name) || []).concat(year));
}
const res = Array.from(map, ([name, year]) => ({name, year}));
console.log(res);
.as-console-wrapper { max-height: 100% !important;} /* ignore */ 

Comments

1

You can achieve what you want with a few steps. Create a temporary map (using a plain object or a Map class). Fill that map using even and odd indexes (even indexes become key and the odd indexes become part of the array) and then convert the map into an array.

const sourceArray = [
    "Spain",
    "2010",
    "Brazil",
    "1994",
    "Brazil",
    "1970",
    "Brazil",
    "1962",
    "Brazil",
    "2002",
    "Brazil",
    "1958",
    "Germany",
    "2014",
    "Germany",
    "1990",
    "Germany",
    "1974",
    "Germany",
    "1954",
    "Italy",
    "2006",
    "Italy",
    "1982",
    "Italy",
    "1938",
    "Italy",
    "1934",
    "France",
    "2018",
    "France",
    "1998",
    "Argentina",
    "1986",
    "Argentina",
    "1978",
    "Uruguay",
    "1930",
    "Uruguay",
    "1950",
    "England",
    "1966"
];

const map = {};

for(let i =0; i < sourceArray.length;i+=2){
  const country = sourceArray[i];
  if(map[country]){
      map[country].push(+sourceArray[i+1])
  }else{
     map[country] = [+sourceArray[i+1]]
  }
}

const finalArray = Object.entries(map).map(([k,v])=> ({ name:k, years :v}))
console.log(finalArray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.