业务中需求的方法,接口返回一个数组,里面包含了大量的对象,具有同名的属性名,比较常见。但是需要将其中参数为 name 的属性值全部取出,合并成数组。 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"
...
继续阅读
(11)