2

The Object is this -

clients = [
  [{
      "label": "MongoDb",
      "value": "5ee71c494be8d0180c1b63d6"
    },
    {
      "label": "Hawlett",
      "value": "5ee71b5e4be8d0180c1b63d2"
    },
    {
      "label": "Coca Cola",
      "value": "5ee7195ef2d5e0175c5da986"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ]
]

I want to make the array in the form shown below to get a new array of object ids to perform further task -

expected = ["5ee71c494be8d0180c1b63d6","5ee71b5e4be8d0180c1b63d2","5ee7195ef2d5e0175c5da986" ....]

Any help would be appreciated. I'm new to Javascript. I hope there's a way to do this in less code.

2
  • 3
    Hint: Look into Array.map Commented Jun 15, 2020 at 8:50
  • Thank you, Rajesh and thanks a lot for editing my question I am really new into this. Commented Jun 15, 2020 at 9:03

3 Answers 3

2

Use flatMap (place all objects into one array) then map the value property into your array.

const clients = [
  [{
      "label": "MongoDb",
      "value": "5ee71c494be8d0180c1b63d6"
    },
    {
      "label": "Hawlett",
      "value": "5ee71b5e4be8d0180c1b63d2"
    },
    {
      "label": "Coca Cola",
      "value": "5ee7195ef2d5e0175c5da986"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ]
]

const res = clients
  .flatMap(arr => arr)
  .map(({value}) => value)

console.log(res)

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

1 Comment

Thanks a lot, @Eugen. I am new to this and really overwhelmed by the response the community gave.
2

Well use .flat() and then .map()

let clients = [
  [{
      "label": "MongoDb",
      "value": "5ee71c494be8d0180c1b63d6"
    },
    {
      "label": "Hawlett",
      "value": "5ee71b5e4be8d0180c1b63d2"
    },
    {
      "label": "Coca Cola",
      "value": "5ee7195ef2d5e0175c5da986"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ],
  [{
      "label": "Tarun infotech",
      "value": "5ee71cc94be8d0180c1b63db"
    },
    {
      "label": "Sahara",
      "value": "5ee71c884be8d0180c1b63d9"
    }
  ]
]

let result = clients.flat().map(el => el.value);

console.log(result);

Comments

1

You can use flatMap() and map():

clients = [
    [
        {
            "label": "MongoDb",
            "value": "5ee71c494be8d0180c1b63d6"
        },
        {
            "label": "Hawlett",
            "value": "5ee71b5e4be8d0180c1b63d2"
        },
        {
            "label": "Coca Cola",
            "value": "5ee7195ef2d5e0175c5da986"
        }
    ],
    [
        {
            "label": "Tarun infotech",
            "value": "5ee71cc94be8d0180c1b63db"
        },
        {
            "label": "Sahara",
            "value": "5ee71c884be8d0180c1b63d9"
        }
    ],
    [
        {
            "label": "Tarun infotech",
            "value": "5ee71cc94be8d0180c1b63db"
        },
        {
            "label": "Sahara",
            "value": "5ee71c884be8d0180c1b63d9"
        }
    ]
];

let values = clients.flatMap(c => c.map(a => a.value));
console.log(values);

1 Comment

Thank you @Rahul Bhobe

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.