0

I am looking for best ways of doing this. I have two arrays (array 1d and array 2d object):

let keys = [11, 12];
let values = [
[
   { name: '1', link: '1', history: '1' },
   { name: '2', link: '2', history: '2' }
 ],
 [
   { name: '3', link: '3', history: '3' },
   { name: '4', link: '4', history: '4' }
 ]
]

The end result I want is an array of map:

[
   { name: '1', link: '1', history: '1' , q : 11},
   { name: '2', link: '2', history: '2' , q : 11},
   { name: '3', link: '3', history: '3' , q : 12},
   { name: '4', link: '4', history: '4' , q : 12}
]

How do I do it the most efficient/clean way using lodash? Thanks!

This code without lodash :

let arr = [];
let keys = [ 29, 30 ]
let values = [
  [
    { name: '1', link: '1', history: '1' },
    { name: '2', link: '2', history: '2' }
  ],
  [
    { name: '3', link: '3', history: '3' },
    { name: '4', link: '4', history: '4' }
  ]
]
for (let j = 0; j < values.length; j++) {
  for (let i = 0; i < values[j].length; i++) {
    arr.push({ ...values[j][i], q: keys[j] });
  }
}
console.log(arr)

7
  • 2
    why do you need lodash? Commented Mar 7, 2020 at 18:05
  • I'm worried about the time if so much information. Commented Mar 7, 2020 at 18:06
  • wait pls i think i ask wrong . Commented Mar 7, 2020 at 18:07
  • @NinaScholz I edit it . Commented Mar 7, 2020 at 18:11
  • 1
    How did you decide to map which keys go to which array? Commented Mar 7, 2020 at 18:15

3 Answers 3

2

You may simply just use _.map

let keys = [11, 12];
let values = [[{"name":"1","link":"1","history":"1"},{"name":"2","link":"2","history":"2"}],[{"name":"3","link":"3","history":"3"},{"name":"4","link":"4","history":"4"}]]
console.log(_.map(values, (v, i) => _.map(v, o => (o.q = keys[i], o))))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js"></script>

or without lodash which is pretty much the same syntax wise

let keys = [11, 12];
let values = [[{"name":"1","link":"1","history":"1"},{"name":"2","link":"2","history":"2"}],[{"name":"3","link":"3","history":"3"},{"name":"4","link":"4","history":"4"}]]
console.log(values.map((v, i) => v.map(o => (o.q = keys[i], o))))

edit: I forgot that the resulting array has to be 1D. - The lodash variation needs to get some _.flatten over the result - The native js needs a .flat()

Lastly one may use lodash/fp to get some chaining style

let keys = [11, 12];
let values = [[{"name":"1","link":"1","history":"1"},{"name":"2","link":"2","history":"2"}],[{"name":"3","link":"3","history":"3"},{"name":"4","link":"4","history":"4"}]]

console.log(
  _.flow(
    // we need to use entries to get the index of fp.map
    _.entries,
    // for each array
    _.map(([i, v]) => 
      // foreach "history" of the array
      _.map(
        // merge the key to the current "history"
        _.merge({ q: keys[i] }), v)
      ),
    _.flatten
  )(values)
)

console.log(
  _.flow(
    // or its variant
    _.map.convert({ cap: false })((v,i) => _.map(_.merge({ q: keys[i] }), v)),
    _.flatten
  )(values)
)
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js"></script>

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

3 Comments

I have not been precise enough but the resulting array is [[],[]], if you want a 1d array you may just use flat() at the end @sir air
i using _.flatMap(result) , i work for me. thanks again @grodzi
you are welcome @sir air I am also adding an example using lodash/fp which people seem to like to work with (I do not share this opinion but this may be useful to you)
1

Not a lodash solution, but a universal approach for more arrays with objects for merging to get a cartesian product.

let keys = [11, 12],
    values = [[{ name: '1', link: '1', history: '1' }, { name: '2', link: '2', history: '2' }], [{ name: '3', link: '3', history: '3' }, { name: '4', link: '4', history: '4' }]],
    data = [
        values.flat(),
        keys.map(q => ({ q }))
    ],
    result = data.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => ({ ...v, ...w }))), []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

You can use Array.flatMap() (or lodash's _.flatMap()) to iterate the values, and then map the sub-arrays, and add the respective key:

const keys = [11, 12];
const values = [[{"name":"1","link":"1","history":"1"},{"name":"2","link":"2","history":"2"}],[{"name":"3","link":"3","history":"3"},{"name":"4","link":"4","history":"4"}]]

const result = values.flatMap((arr, i) => 
  arr.map(o => ({ ...o, q: keys[i] }))
)

console.log(result)

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.