简体   繁体   English

使用另一个数组的对象键过滤一个对象

[英]Filtering one object with another array's object key

I am trying to filter an object with another object inside of an array.我试图用数组中的另一个对象过滤一个对象。

To be more precise, I am trying to compare the keys of the object inside the array, to the keys of my main object.更准确地说,我试图将数组内对象的键与主对象的键进行比较。 If the values are the same, I want to return the value corresponding to those keys.如果值相同,我想返回与这些键对应的值。

Here's an example:下面是一个例子:

var a = {
  "maths":"A++",
  "literature":"C-",
  "sports":"B+",
  "biology":"D",
  "chemistry":"A",
  "english":"A+",
  "physics":"C+"
}


var b = [{
  "maths":"Mathematics",
  "biology":"Biology",
  "physics":"Physics"
}]

I wanna check if any of the keys in object b are inside object a and if they are, I want to return their value into array.我想检查是否有在目标的关键b是内部目标a ,如果是这样,我想他们的价值回归到数组。 For example, I want to return ["A++","D","C+"]例如,我想返回["A++","D","C+"]

I've tried using filter and Array.prototype.some but I couldn't figure out anything.我试过使用filterArray.prototype.some但我什么也想不通。 Any advice on how should I achieve this?关于我应该如何实现这一目标的任何建议?

First make an array or Set of all the keys inside b , then use .map to access each key on the a object:首先创建b中所有键的数组或 Set,然后使用.map访问a对象上的每个键:

 var a = { "maths":"A++", "literature":"C-", "sports":"B+", "biology":"D", "chemistry":"A", "english":"A+", "physics":"C+" } var b = [{ "maths":"Mathematics", "biology":"Biology", "physics":"Physics" }]; const keys = b.flatMap(Object.keys); const arr = keys.map(key => a[key]); console.log(arr);

If you are dealing with a single object in the array b , then you can do this:如果您正在处理数组b的单个对象,那么您可以这样做:

 var a = { "maths":"A++", "literature":"C-", "sports":"B+", "biology":"D", "chemistry":"A", "english":"A+", "physics":"C+" } var b = [{ "maths":"Mathematics", "biology":"Biology", "physics":"Physics" }] const valuesInAndB = Object.keys(a).reduce((acc,x) => { if (b[0][x]) { return acc.concat(a[x]); } return acc; }, []); console.log(valuesInAndB);

However, if the objects in b will be greater than one then as answered by @certainperformance you could get all the keys in b and map through a with those keys.但是,如果b的对象大于 1,那么正如@certainperformance 所回答的那样,您可以获取b所有键并使用这些键映射a

const keysInB = b.flatMap(Object.keys);
keysInB.map(key => a[key]);

flatMap is not available in some older browsers, please keep that in mind. flatMap在某些较旧的浏览器中不可用,请记住这一点。

I'm assuming that you want to handle multiple objects in b.我假设你想在 b 中处理多个对象。 If so and if you want one array for each object in b then you could do something like:如果是这样并且如果您想要 b 中的每个对象一个数组,那么您可以执行以下操作:

 var a = { "maths":"A++", "literature":"C-", "sports":"B+", "biology":"D", "chemistry":"A", "english":"A+", "physics":"C+" } var b = [{ "maths":"Mathematics", "biology":"Biology", "physics":"Physics" },{ "maths":"Mathematics", "biology":"Biology", "english":"English" }] const result = b.map(obj => Object.keys(obj).map(key => a[key])); console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM