JavaScript How do I can split an array of objects that contains another arrays by Lodash split array objects,split array objects javascript,split objects array

I have this array: [{ name: "A1", arr: [1,2] }, { name: "A2", arr: [1] }]

How do I can split the first one object to two objects that should contains only one array value?

The result should be:

[{name: "A1", arr: [1] }, {name: "A1", arr: [2]}, {name: "A2", arr: [1] }]

I didn't find anything universal tool that can help to resolve this issue in the lodash documentation. (Actually I use lodash very rare so that's why I asked to help me).

Thanks for any help!

Answer:1

Loop the data using Array#map, create the separate objects using a 2nd Array#map, and then flatten them by spreading the results and using Array#concat on them:

var data = [{ name: "A1", arr: [1,2] }, { name: "A2", arr: [1] }];

var result = [].concat(...data.map((item) => item.arr.map((num) => Object.assign({}, item, { arr: [num] }))));

console.log(result);
Answer:2

You could map the items of the array in an object and map the outer name property to the new result set, as this answer of ryeballar of Flatten an nested array of objects using Lodash suggest.

var data = [{ name: "A1", arr: [1, 2] }, { name: "A2", arr: [1] }],
    result = _.flatMap(data, 
        item => _.map(_.map(item.arr,
            v => ({ arr: [v] })
        ), prop => _.defaults({ name: item.name }, prop)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Answer:3

I am facing issue regarding passing javascript variables to php within same function. My code looks like this else if(msg_type[i] == 'code' ){ var code_action = 'test'; <?php function ...

I am facing issue regarding passing javascript variables to php within same function. My code looks like this else if(msg_type[i] == 'code' ){ var code_action = 'test'; <?php function ...

  1. passing variable from html to javascript
  2. passing variable from javascript to php
  3. passing variable from shell script to jenkins
  4. passing variable from controller to view laravel
  5. passing variable from response to request in jmeter
  6. passing variable from parent to child angular
  7. passing variable from html to php
  8. passing variable from javascript to python
  9. passing variable from js to php
  10. passing variable from js to django view
  11. passing variable from html to node js
  12. passing variable from html to python
  13. passing variable from flask to javascript

I'm trying to get a React component I've built to display on the same line as the rest of the text, but it keeps appearing on a different line than the rest of the elements. Here's my React code: &...

I'm trying to get a React component I've built to display on the same line as the rest of the text, but it keeps appearing on a different line than the rest of the elements. Here's my React code: &...

For some reason I can't seem to find where by syntax error would be in this Lodash function. Does anyone know where I'm going wrong here? const classes = _.filter(state.classes, class => { ...

For some reason I can't seem to find where by syntax error would be in this Lodash function. Does anyone know where I'm going wrong here? const classes = _.filter(state.classes, class => { ...

  1. javascript lodash syntax

How can i recursively search a nested object to find the PATH to an object reference I'm providing? My original object looks like: a = { b: [ { children: [...more objects] }, { children: [] } ...

How can i recursively search a nested object to find the PATH to an object reference I'm providing? My original object looks like: a = { b: [ { children: [...more objects] }, { children: [] } ...

  1. javascript find path in tree
  2. javascript find path
  3. javascript find path to key
  4. javascript find path file
  5. javascript find shortest path
  6. javascript find root path
  7. javascript find current path
  8. javascript find relative path
  9. javascript find url path
  10. javascript find folder path
  11. javascript json find path
  12. javascript find full path
  13. javascript find base path
  14. javascript find element by path
  15. javascript find filename from path
  16. javascript find current file path
  17. javascript find object by path
  18. javascript tree find node path