1

currently I have a little problem, which I am not quite able to solve. I have an array, which can contain single, or more arrays, which than can contain multiple objects. So for instance:

const arr = [
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ],
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ]
]

And I want to merge the objects based on a specific property (here "value") into an array, so that my result will look like that:

const result = ["value A", "value B", "value C", "value A", "value B", "value C"]

Someone got an idea for that? Thank you!

3
  • 2
    Can you confirm your expected output, please? You wrote const result = {"value A", "value B", "value C", "value A", "value B", "value C"} (which is not valid JS) altought I assume you meant const result = ["value A", "value B", "value C", "value A", "value B", "value C"] Commented Feb 3, 2021 at 10:49
  • Your result is not possible as you depict an object with only values and no keys. Did you mean to use an array? Commented Feb 3, 2021 at 10:50
  • Ooopsie, yeah, my result should be an array of course. Thanks! Commented Feb 3, 2021 at 10:53

4 Answers 4

3

const arr = [
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ],
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ]
]

let x = arr.flat().map(v=>v.value);

console.log(x);

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

1 Comment

Dude, thats awesome! Works like a charm. I have to look into, what flat actually does. Thank you :)
0

I believe your desired result is an array, and not object. The output example of yours is invalid.

Anyway, use simple reduce:

const arr = [
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ],
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ]
]

const result = arr.reduce((acc, cur) => {
  const values = cur.map(({value}) => value);
  return acc.concat(values)
}, []);

console.log(result);

Comments

0

Like this?

const arr = [
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ],
  [
    {
      "value": "value A"
    },
    {
      "value": "value B"
    },
    {
      "value": "value C"
    }
  ]
]

function mergeArray( arr, property ) {
  const res = [];
  for (const innerArr of arr) {
    for (const obj of innerArr) {
      res.push(obj[property]);
    }
  }
  return res;
}

console.log(mergeArray(arr, "value"));

Comments

0

I use nested reduce and map methods of the Array.prototype

{
  const arr = [
    [{
        "value": "value A"
      },
      {
        "value": "value B"
      },
      {
        "value": "value C"
      }
    ],
    [{
        "value": "value A"
      },
      {
        "value": "value B"
      },
      {
        "value": "value C"
      }
    ]
  ];
  
  const newArr = arr.reduce((carry, item) => {
     carry.push(...item.map(item2 => item2.value))
     return carry;
  }, [])
  
  console.log(newArr)
}

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.