2

Give an array of objects as below:

var objArray = [{'key': 'key1', 'fields': [1, 2]}, {'key': 'key2', 'fields': [3, 4]}];

I would like to get an array of merged items from fields property and output should be 1, 2, 3, 4

I've tried to merge as below. I'm not satisfied with my solution and would like to get feedback's and improve upon my solution. I'm new to JS and typescript. I believe there is better solution with features of ES6. Can someone help to improve my solution?

function mergeArraysFromObjectArray(objArray){
   let allFields = [];
    for (let obj of objArray) {
      for (let propt in obj) {
        if (obj.hasOwnProperty(propt) && propt === 'fields') {
          allFields = allFields.concat(obj[propt]);
        }
      }
    } 

  return allFields;
}

var objArray = [{'key': 'key1', 'fields': [1, 2]}, {'key': 'key2', 'fields': [3, 4]}];
let allMergedFields = mergeArraysFromObjectArray(objArray);

for(let i=0; i < allMergedFields.length; i++){
   console.log(allMergedFields[i]);  //prints - 1, 2, 3, 4
}
4
  • 1
    For working code that you want reviewed, see codereview.stackexchange.com Commented Aug 30, 2017 at 12:24
  • Check the answer it is what you exactly want Commented Aug 30, 2017 at 12:25
  • 1
    I'm voting to close this question as off-topic because it's better suited to codereview.stackexchange.com Commented Aug 30, 2017 at 15:45
  • The question that was suggested as off-topic should be moved to codereview.stackexchange.com, not closed as Opinion-based. Commented Sep 21, 2020 at 3:28

3 Answers 3

11

You can use Array.prototype.flatMap()

Code example 1:

const objArray = [{key: 'key1', fields: [1, 2]}, {key: 'key2', fields: [3, 4]}]
const result = objArray.flatMap(({ fields }) => fields)

console.log(result)

Also you can use Array.prototype.map() and Array.prototype.flat()

Code example 2:

const objArray = [{key: 'key1', fields: [1, 2]}, {key: 'key2', fields: [3, 4]}]
const result = objArray.map(({ fields }) => fields).flat()

console.log(result)

Also you can use Array.prototye.reduce() combined with destructuring assignment and spread syntax

Code example 3:

const objArray = [{key: 'key1', fields: [1, 2]}, {key: 'key2', fields: [3, 4]}]
const result = objArray.reduce((a, { fields }) => [...a, ...fields], [])

console.log(result)

Also using just Array.prototye.reduce() combined with Array.prototype.concat():

Code example 4:

const objArray = [{key: 'key1', fields: [1, 2]}, {key: 'key2', fields: [3, 4]}]
const result = objArray.reduce((a, { fields }) => a.concat(fields), [])

console.log(result)

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

1 Comment

This is how you do it properly :) Es6 baby :) Good job
5

You can simply concat the array and create a new array inside a loop:

var objArray = [{'key': 'key1', 'fields': [1, 2]}, {'key': 'key2', 'fields': [3, 4]}];

var result = [];
for(var i=0; i<objArray.length; i++){
   result = result.concat(objArray[i].fields);
}

console.log(result);

Comments

3

You can use spread syntax ... and map() to get fields property

var objArray = [{'key': 'key1', 'fields': [1, 2]}, {'key': 'key2', 'fields': [3, 4]}];
var result = [].concat(...objArray.map(({fields}) => fields))
console.log(result)

You can also add filter(Boolean) to remove undefined in case there is no fields property.

var objArray = [{'key': 'key1', 'asdf': [1, 2]}, {'key': 'key2', 'fields': [3, 4]}];
var result = [].concat(...objArray.map(e => e.fields).filter(Boolean))
console.log(result)

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.