0

I have an array how build like this:

"list": [
    {
        "insideList": [{"code": "0", "image": "./images/1"},
                    {"code": "1", "image": "./images/2"},
                    {"code": "2", "image": "./images/3"}],
        "bucket": "info",
        "instructions": "first set".\n",
        "color": "rgb(242, 242, 140)",
        "id": "a",
        "text": "first quest"
    },
   ...

I'm trying to flatten this list with lodash, but I dont know how to map inside map when the first map also do things. that what i did for the outer loop:

var withIndex = _.map(list,(item, i) => ({

      [`iv_${i}_text`]: item.text,
      [`iv_${i}_instructions`]: item.instructions,
      [`iv_${i}_color`]: item.color,
      [`iv_${i}_id`]: item.id,
      [`iv_${i}_bucket`]: item.bucket,
    }));

but now i want to add the inside loop on 'aspects' list and build something like this:

 var withIndex = _.map(list,(item, i) => ({
  [`iv_${i}_text`]: item.text,
  [`iv_${i}_instructions`]: item.instructions,
  [`iv_${i}_color`]: item.color,
  [`iv_${i}_id`]: item.id,
  [`iv_${i}_bucket`]: item.bucket,
  [`iv_${i}_a_0_code`]: item.insideList[0].code,       
  [`iv_${i}_a_0_image`]: item.insideList[0].image,  
  [`iv_${i}_a_1_code`]: item.insideList[1].code,       
  [`iv_${i}_a_1_image`]: item.insideList[1].image,
  [`iv_${i}_a_2_code`]: item.insideList[2].code,       
  [`iv_${i}_a_2_image`]: item.insideList[2].image
}));

It is possible to do something like this using lodash?

var withIndex = _.map(list,(item, i) => ({
      [`iv_${i}_text`]: item.text,
      [`iv_${i}_instructions`]: item.instructions,
      [`iv_${i}_color`]: item.color,
      [`iv_${i}_id`]: item.id,
      [`iv_${i}_bucket`]: item.bucket,

and here do another map:

 _map(insideLoop, (item, j)=> ({
      [`iv_${i}_a_{j}_code`]: item.insideList[0].code,       
      [`iv_${i}_a_{j}_image`]: item.insideList[0].image,  
      [`iv_${i}_a_{j}_code`]: item.insideList[1].code,       
      [`iv_${i}_a_{j}_image`]: item.insideList[1].image,
      [`iv_${i}_a_{j}_code`]: item.insideList[2].code,       
      [`iv_${i}_a_{j}_image`]: item.insideList[2].image
    }));

1 Answer 1

2

Reducing (accumulating) object properties should do the thing, then just merge both objects:

var withIndex = _.map(list, (item, i) => _.assign(
  {
    [`iv_${i}_text`]: item.text,
    [`iv_${i}_instructions`]: item.instructions,
    [`iv_${i}_color`]: item.color,
    [`iv_${i}_id`]: item.id,
    [`iv_${i}_bucket`]: item.bucket,
  },
  _.reduce(item.insideList, (acc, x, j) => {
    acc[`iv_${i}_a_${j}_code`]: x.code
    acc[`iv_${i}_a_${j}_image`]: x.image
    return acc
  }, {})
));

Also you don't need lodash after all (native functions are faster):

var withIndex = list.map((item, i) => Object.assign(
  {
    [`iv_${i}_text`]: item.text,
    [`iv_${i}_instructions`]: item.instructions,
    [`iv_${i}_color`]: item.color,
    [`iv_${i}_id`]: item.id,
    [`iv_${i}_bucket`]: item.bucket,
  },
  item.insideList.reduce((acc, x, j) => {
    acc[`iv_${i}_a_${j}_code`]: x.code
    acc[`iv_${i}_a_${j}_image`]: x.image
    return acc
  }, {})
));
Sign up to request clarification or add additional context in comments.

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.