如何获得数组值和对象键值匹配的顺序

时间:2019-01-27 20:05:15

标签: javascript arrays object ecmascript-6

在值数组中

var legends =['some','key', 'value'];

具有相应obj值的图;

var graph = {
  orgid: ['123', '123556', '456', '345', '2345'],
  some: [1500, 1500, 1500, 1500, 1500],
  key: [900, 900, 900, 900, 900],
  value: [1072, 1373, 946, 715, 276]
 };

我想提取仅包含数组中提到的键值的图形以插入到另一个数组中

我已经执行了代码,但是仅当键的顺序与obj中提到的顺序相同时,它才起作用

es5方式

var grphFilter = [];
var countRaw = 0; 
for (var key in graph) {
  if (key === legends[count] ) {
    grphFilter.push(graph[key]);
    count++;
  }
}

es6方式

let grphFilter = [];
let count = 0; 
const result = Object.entries(graph).forEach(function([key, value]) {
  if (key === legends[count]) {
    grphFilter.push(value);
    count++
  }
})

,但不适用于顺序不正确的键。

var legendsNotOrder =['key', 'some', 'value'];

与图例数组中值的顺序无关,结果应为最终结果,无论是legends还是legendsNotOrder

最终结果

var graphfiltered = [
  [1500, 1500, 1500, 1500, 1500],
  [900, 900, 900, 900, 900],
  [1072, 1373, 946, 715, 276]
];

5 个答案:

答案 0 :(得分:4)

您可以遍历数组并从对象中仅获取所需的属性值。

var legends =['some','key', 'value'];

var graph = {orgid: ['123', '123556', '456', '345', '2345'],some: [1500, 1500, 1500, 1500, 1500],key: [900, 900, 900, 900, 900],value: [1072, 1373, 946, 715, 276]};
 
let filteredGraph = legends.map( cur => graph[cur])

console.log(filteredGraph)


//In case you have keys in legend which are not available in graph you can use below code

let filteredGraph = legends.filter( cur => graph[cur])

答案 1 :(得分:1)

使用Object#entries,Array#reduce,Array#includes,Array#push

const legends=['some','key','value'];const graph={orgid:['123','123556','456','345','2345'],some:[1500,1500,1500,1500,1500],key:[900,900,900,900,900],value:[1072,1373,946,715,276]}

const res = Object.entries(graph)
.reduce((a,[k,v])=>{
  if(legends.includes(k)) a.push(v);
  return a;
}, []);

console.log(res);

答案 2 :(得分:0)

您可以使用keys在数组上循环,然后直接访问graph对象。另外,您可以在对新数组执行valid之前检查键是否为push

var legends = ['some', 'key', 'value', 'notkey'];

var graph = {
    orgid: ['123', '123556', '456', '345', '2345'],
    some: [1500, 1500, 1500, 1500, 1500],
    key: [900, 900, 900, 900, 900],
    value: [1072, 1373, 946, 715, 276]
 };
 
 let graphFilter = [];
 legends.forEach(x => graph[x] && graphFilter.push(graph[x]));
 console.log(graphFilter);

另一种替代方法是在图形的条目上组合filter()map()

var legends = ['some', 'key', 'value', 'notkey'];

var graph = {
    orgid: ['123', '123556', '456', '345', '2345'],
    some: [1500, 1500, 1500, 1500, 1500],
    key: [900, 900, 900, 900, 900],
    value: [1072, 1373, 946, 715, 276]
 };
 
let graphFilter = Object.entries(graph)
     .filter(([k, v]) => legends.includes(k))
     .map(([k, v]) => v);

console.log(graphFilter);

答案 3 :(得分:0)

问题是您在key数组中使用特定值在当前迭代中检查legends。您需要检查的是当前迭代中的key是否等于此数组的任何值。

类似的东西:

legends.includes(key)

这是一个完整的示例:

var legends = ['some', 'key', 'value'];

var graph = {
  orgid: ['123', '123556', '456', '345', '2345'],
  some: [1500, 1500, 1500, 1500, 1500],
  key: [900, 900, 900, 900, 900],
  value: [1072, 1373, 946, 715, 276]
};

var graphFilter = [];
for (var key in graph) {
  if (legends.includes(key)) {
    graphFilter[legends.indexOf(key)] = graph[key];
  }
}

console.log(graphFilter);

答案 4 :(得分:0)

ES6 一种带有reduce的循环解决方案,它忽略了legends数组中的未知键:

const legends = ['some', 'key', 'value', 'random_key'];

const graph = {
    orgid: ['123', '123556', '456', '345', '2345'],
    some:  [1500, 1500, 1500, 1500, 1500],
    key:   [900, 900, 900, 900, 900],
    value: [1072, 1373, 946, 715, 276]
};

const result = legends.reduce((a, c) => {
    if (r = graph[c]) {
        a.push(r);
    }
    return a;
}, []);

console.log(result);

通常,map + filter通常可以用reduce替换。值得关注的是,代码可能会变得更加复杂。好处可能是优化(可能是“微”),这通常是不值得的。

请注意,我在原始代码中将var替换为const