0

I web API call and in the background, it's running more than one time. So I received the response commentRows as data which I mention.

I have to map those data into another array.

var arrPush =[ ]; 
var commentRows ={  
   '@odata.context':'https:indexes',
   value:[  
      {  
         "key":"176611",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      },
      {  
         "key":"176100",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      }
   ]
}; 

arrPush.push(commentRows.value);

It generates the array for as,

[  
   [  
      {  
         "key":"176611",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      },
      {  
         "key":"176100",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      }
   ]
]

Needed

   [  
      {  
         "key":"176611",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      },
      {  
         "key":"176100",
         "status":true,
         "errorMessage":null,
         "statusCode":200
      }
   ]

but I don't want the first [] has to be appended Can I use any Lodash for achieving this

3
  • updated question Commented Jun 27, 2018 at 8:11
  • arrPush.push(commentRows.value[0]); Do not use lodash for this Commented Jun 27, 2018 at 8:11
  • it may be [{"key":"176611","status":true,"errorMessage":null,"statusCode":200},{"key":"176100","status":true,"errorMessage":null,"statusCode":207}] sometime Commented Jun 27, 2018 at 8:13

4 Answers 4

3

Here is an solution which can handle multiple elements in value array:

use _.flatten:

var arrPush = [];
var commentRows = {  
       '@odata.context':'https:indexes',
       value:[  
          {  
             "key":"176611",
             "status":true,
             "errorMessage":null,
             "statusCode":200
          }
       ]
    };

arrPush.push(commentRows.value);
arrPush = _.flatten(arrPush);  // here you get it
Sign up to request clarification or add additional context in comments.

Comments

1

var arrPush = [];
var commentRows = {  
  '@odata.context':'https:indexes',
  value:[  
    {  
       "key":"176611",
       "status":true,
       "errorMessage":null,
       "statusCode":200
    }
  ]
};

commentRows.value.forEach(x => arrPush.push(x))
console.log(arrPush);

Use a forEach loop and push them into arrPush

Comments

0

Just push like this arrPush.push(commentRows.value[0]);

Comments

-1

You can use

arrPush = _.concat(arrPush, commentRows.value[0]).

Here arrayToCopy is the array to which you want to copy the data.

1 Comment

You didn't declared arrayToCopy. Right?

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.