angularjs 在一个数组中过滤多个键值(对象的数组)

w3nuxt5m  于 12个月前  发布在  Angular
关注(0)|答案(2)|浏览(97)

我有一个键和值对格式,我想过滤数组中的所有值到一个数组中

const holiday_expenses = {
  food: [{name: "abc", place: "xyz"}],
  travel: [{name: "abcd", place: "xyz1"}],
  accommodation: [{name: "xysz", place: "xyz2"}]
}

字符串
预期结果:

result : [{name: "abc", place: "xyz"}, {name: "abcd", place: "xyz1"}, {name: "xysz", place: "xyz2"}]

w1jd8yoj

w1jd8yoj1#

只需要得到所有的object's valuesflatten

const result = Object.values(holiday_expenses).flat();

字符串

完整片段:

const holiday_expenses = {
  food: [{name: "abc", place: "xyz"}],
  travel: [{name: "abcd", place: "xyz1"}],
  accommodation: [{name: "xysz", place: "xyz2"}]
};

const result = Object.values(holiday_expenses).flat();

console.log(result);

gk7wooem

gk7wooem2#

import "_" from lodash;
cosnt obj = {/...whatever you have/}
const res = _.values(obj);

字符串
检查“lodash”,我相信它可以让你的生活更好。https://lodash.com/docs/有很多帮助函数来操作数组和对象。

相关问题