Take out the property values ​​of multiple objects with the same name in the array and merge them into a new array

Original link: https://zburu.com/archives/168.html

The method required in the business, the interface returns an array, which contains a large number of objects, with the property name of the same name, which is more common than arrogance. However, it is necessary to take out all the attribute values ​​whose parameter is name and combine them into an array.

 const num = [ { id: 1, name: 'abc', }, { id: 2, name: 'xyz', } ] function getFields(arrnum, field) { const resnum = []; for (let i = 0; i < arrnum.length; ++i) resnum.push(arrnum[i][field]); return resnum; } const result = getFields(num, "name"); console.log(result); // ['abc', 'xyz'] console.log(result.join(' ')); // "abc xyz"

This article is reproduced from: https://zburu.com/archives/168.html
This site is for inclusion only, and the copyright belongs to the original author.