使用过滤器或查找方法在JSON中检索对象

发布于 2025-02-03 07:05:26 字数 1488 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2025-02-10 07:05:26

查找将为您提供满足提供的测试功能的提供的数组中的第一个元素。
过滤器将为您提供一个新数组,其中包含满足提供的测试功能的所提供数组中的所有元素。

简化:

const rates = [{
    'id': 'T300Mb2',
    'fiberAgrupations': ['3230', '3232']
  },
  {
    'id': 'T600Mb',
    'fiberAgrupations': ['2989', '2994']
  },
  {
    'id': 'T600Mb2',
    'fiberAgrupations': ['3219', '3220'],
  }];

const myPackId = '3230';
const result = rates.filter(landlinedRates =>
 landlinedRates['fiberAgrupations'].includes(myPackId)
);
console.log(result);

给予:

[ 
   { 
    id: 'T300Mb2', 
    fiberAgrupations: [ '3230', '3232' ]
   } 
]

Find will give you first element in the provided array that satisfies the provided testing function.
Filter will give you a new array with all elements in the provided array that satisfies the provided testing function.

Simplified:

const rates = [{
    'id': 'T300Mb2',
    'fiberAgrupations': ['3230', '3232']
  },
  {
    'id': 'T600Mb',
    'fiberAgrupations': ['2989', '2994']
  },
  {
    'id': 'T600Mb2',
    'fiberAgrupations': ['3219', '3220'],
  }];

const myPackId = '3230';
const result = rates.filter(landlinedRates =>
 landlinedRates['fiberAgrupations'].includes(myPackId)
);
console.log(result);

gives:

[ 
   { 
    id: 'T300Mb2', 
    fiberAgrupations: [ '3230', '3232' ]
   } 
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文