Как поместить все объекты в другой массив, используя JavaScript Lodash - PullRequest
0 голосов
/ 27 июня 2018

У меня веб-вызов API, и в фоновом режиме он выполняется более одного раза. Поэтому я получил ответ commentRows в качестве данных, о которых я упоминаю.

Мне нужно отобразить эти данные в другой массив.

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);

Генерирует массив для as,

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

Необходимый

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

но я не хочу, чтобы добавлялся первый []. Могу ли я использовать любой Lodash для достижения этого

Ответы [ 4 ]

0 голосов
/ 27 июня 2018

Вот решение, которое может обрабатывать несколько элементов в массиве value:

использовать _. 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
0 голосов
/ 27 июня 2018

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);

Используйте петлю forEach и вставьте их в arrPush

0 голосов
/ 27 июня 2018

Вы можете использовать

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

Здесь arrayToCopy - массив, в который вы хотите скопировать данные.

0 голосов
/ 27 июня 2018

Просто нажмите как arrPush.push (commentRows.value [0]);

...