简体   繁体   English

如何从 object 中提取引脚属性并在 JavaScript 中制作引脚数组?

[英]How can I extract pin property from an object and make an array of pins in JavaScript?

I have done it with array PinArray.push(data.pin) , but I am looking for a better solution.我已经用数组PinArray.push(data.pin)完成了它,但我正在寻找更好的解决方案。

let PinArray = [];

const bbb = AllPackages.forEach(p => {
    p.dealProducts
        .filter(dp => dp.product.code === 'AAA')
        .forEach(data => PinArray.push(data.pin));
});

 const AllPackages = [ { Id: 1, dealProducts: [ { pin: 'AAA000', product: { id: '100', code: 'AAA', name: 'AAA' } }, { pin: 'AAA111', product: { id: '200', code: 'BBB', name: 'BBB' } } ] }, { Id: 2, dealProducts: [ { pin: '', product: { id: '300', code: 'CCC', name: 'CCC' } }, { pin: '1', product: { id: '200', code: 'AAA', name: 'BBB' } }, { pin: '1', product: { id: '200', code: 'BBB', name: 'BBB' } }, { pin: '', product: { id: '400', code: 'DDD', name: 'DDD' } }, { pin: 'AAA111', product: { id: '100', code: 'AAA', name: 'AAA' } } ] } ]; const PinArray = AllPackages.forEach(p => { p.dealProducts.filter(dp => dp.product.code === 'AAA').forEach(data => { const{pin} = data; return pin; }); }); console.log(PinArray); <:-- begin snippet: js hide: false console: true babel: false -->

You can concisely get a single array of all the dealProducts with flatMap .您可以使用flatMap简洁地获取所有dealProducts的单个数组。 Then .filter to select only those with the right code, then map to extract the pin from each:然后.filter到 select 只有那些具有正确代码的,然后 map 从每个提取引脚:

const pinArray = AllPackages
  .flatMap(p => p.dealProducts)
  .filter(obj => obj.product.code === 'AAA')
  .map(data => data.pin);

 const AllPackages = [{ Id: 1, dealProducts: [{ pin: 'AAA000', product: { id: '100', code: 'AAA', name: 'AAA' } }, { pin: 'AAA111', product: { id: '200', code: 'BBB', name: 'BBB' } } ] }, { Id: 2, dealProducts: [{ pin: '', product: { id: '300', code: 'CCC', name: 'CCC' } }, { pin: '1', product: { id: '200', code: 'AAA', name: 'BBB' } }, { pin: '1', product: { id: '200', code: 'BBB', name: 'BBB' } }, { pin: '', product: { id: '400', code: 'DDD', name: 'DDD' } }, { pin: 'AAA111', product: { id: '100', code: 'AAA', name: 'AAA' } } ] } ]; const pinArray = AllPackages.flatMap(p => p.dealProducts).filter(obj => obj.product.code === 'AAA').map(data => data.pin); console.log(pinArray);

Hi you could do something like this:嗨,你可以这样做:

const PinArray3 = AllPackages.flatMap((p) => p.dealProducts).reduce(
   (acc, dealProduct) => (dealProduct.product.code === "AAA" ? [...acc, dealProduct.pin] : acc), 
   []
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JavaScript 中的嵌套数组对象中提取特定属性作为数组 - How can I extract specific property as an array from nested array object in JavaScript 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 如何从 javascript 中的 object 数组中提取特定属性(即键/值)? - how to extract particular property(i.e key/value) from array of object in javascript? 如何使用javascript从对象数组中提取对象的值 - How can I extract the value of an object from an array of objects array using javascript 如何从JavaScript对象提取属性 - How to extract property from JavaScript object 如何在Javascript中使对象属性既是函数又是变量? - How can I make an object property be both a function and a variable in Javascript? 如何将对象的属性值提取到数组中并使数组中的元素不同? - How to extract the property values of a object into an array and make elements in the array different? 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何从Java中的JavaScript脚本提取对象 - How can i extract an object from a javascript script in java 如何从 object 属性制作重复数据删除数组? - How do I make deduped array from object property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM