0

I am looking to merge array inside another array objects in one array in javascript. I tried many way like concat and join methods but I couldn't get it I have array below array which I need to merge into one array

  arr =  [
        [{
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            },
            {
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            }
        ],
        [{
            PRODUCE: 'Apple',
            STATE: 'CP',
            COUNTRY: 'US'
        }]

    ]

I want result as below

[
    {
        PRODUCE: 'Apple',
        STATE: 'CA',
        COUNTRY: 'US'
    },
    {
        PRODUCE: 'Apple',
        STATE: 'CA',
        COUNTRY: 'US'
    },
    {
        PRODUCE: 'Apple',
        STATE: 'CP',
        COUNTRY: 'US'
    }
]
1
  • use Array.flat() Commented Feb 12, 2021 at 17:14

5 Answers 5

2

You can use flat() for this

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Here you can read more about it

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

const arr = [
  [{
      PRODUCE: 'Apple',
      STATE: 'CA',
      COUNTRY: 'US'
    },
    {
      PRODUCE: 'Apple',
      STATE: 'CA',
      COUNTRY: 'US'
    }
  ],
  [{
    PRODUCE: 'Apple',
    STATE: 'CP',
    COUNTRY: 'US'
  }]

]

console.log(arr.flat());

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

1 Comment

thanks @Aalex however couldn't find method flat()
1

If you are able to use modern javascript, you can try:

arr.flat()

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

If not, you can just use lodash, underscore or ramda. Libraries with useful utilities.

https://lodash.com/docs/4.17.15#flatten

Comments

1

You can use Array.flat().

var newArray = arr.flat([depth]);

    const arr =  [
        [{
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            },
            {
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            }
        ],
        [{
            PRODUCE: 'Apple',
            STATE: 'CP',
            COUNTRY: 'US'
        }]

    ]
    console.log(arr.flat())

Comments

1

Simple Approach, using spread operator and forEach loop

const arr =  [
        [{
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            },
            {
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            }
        ],
        [{
            PRODUCE: 'Apple',
            STATE: 'CP',
            COUNTRY: 'US'
        }]

    ]
    let result = [];
    arr.forEach((item)=>{
     if(Array.isArray(item)){
       result = [...result,...item]
     }
     else{
        result = [...result,item]
     }
    }
    )
    console.log(result)

Comments

0

There are two flat() answer, but alternative code that you tried for concat(), you can try like this:

var arr =  [
        [{
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            },
            {
                PRODUCE: 'Apple',
                STATE: 'CA',
                COUNTRY: 'US'
            }
        ],
        [{
            PRODUCE: 'Apple',
            STATE: 'CP',
            COUNTRY: 'US'
        }]

    ]
    
var newArray = arr[1].concat(arr[0]);

console.log(newArray);

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.