2

My Object contains different datatypes including array. I want to concatenate all arrays(in a single array) I have written using foreach and concat.

Is there any way I can have a better solution, or this is correct?

See the below snippet. concat array value3 and value5 into single array.

var Input = {
    "value1": 10,
    "value2": "abcd",
    "value3": [
        {
            "v1": 100,
            "v2": 89
        },
        {
            "v1": 454,
            "v2": 100
        }
    ],
    "value4": "xyz",
    "value5": [
        {
            "v6": 1,
            "v7": -8
        },
        {
            "v1": 890,
            "v2": 10
        }
    ]
}

   
var OutputData = [];
  let keys = Object.keys(Input);
  keys.forEach(key => {
    if (Array.isArray(Input[key])) {
      try {
        OutputData= OutputData.concat(Input[key])
      } catch (error) {
      }
    }
  });
  
console.log(OutputData)

2 Answers 2

3

You could use Array.prototype.filter() with Array.prototype.flat() method get a cleaner code. First get all the values using Object.values() method. Then use filter method to get the arrays and at last use flat method to get a new array with all sub-array elements concatenated.

const input = {
  value1: 10,
  value2: 'abcd',
  value3: [
    {
      v1: 100,
      v2: 89,
    },
    {
      v1: 454,
      v2: 100,
    },
  ],
  value4: 'xyz',
  value5: [
    {
      v6: 1,
      v7: -8,
    },
    {
      v1: 890,
      v2: 10,
    },
  ],
};

const ret = Object.values(input)
  .filter((x) => Array.isArray(x))
  .flat();
console.log(ret);

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

4 Comments

This is the solution which I was looking. Thanks for making it without For loop and cleaner code.
is there any way to add one more in filter v1>10 in one liner. I tried with adding one more condition i.e. && x.v1>10 but it didn't worked. I mean is it possible to add in one line only.
@RajseGodia what will be the output if v1>10?
Actually it will filter all object having v1>10, currently output will be same. I added one more filter after flat and it works fine. Thanks.
1

I think this might be a little bit cleaner to read:

var input = {
  "value1": 10,
  "value2": "abcd",
  "value3": [{
      "v1": 100,
      "v2": 89
    },
    {
      "v1": 454,
      "v2": 100
    }
  ],
  "value4": "xyz",
  "value5": [{
      "v6": 1,
      "v7": -8
    },
    {
      "v1": 890,
      "v2": 10
    }
  ]
};

let res = [];

for (let k in input) {
  if (input.hasOwnProperty(k) && Array.isArray(input[k]))
    input[k].forEach(x => res.push(x));
}

console.log(res);

2 Comments

I think without for loop this can't be done as we have to check isArray. Thank you for making code sweeter.
@hamide niakan, recently got solution without for loop. Please have a look.

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.