如何从值列表中获取密钥?

时间:2017-08-14 13:29:30

标签: javascript list underscore.js

我想根据给定的值列表获取密钥..

这里是我要找的,

这是我的目标,

my_obj = {
  "A": "a_id",
  "B": "b_id",
  "C": "c_id",
  "D": "d_id",
  "E": "status",
  "F": "start_time",
  "G": "end_time",
  "H": "count",
  "I": "task_desc",
  "J": "approved",
  "K": "point",
  "L": "complex",
  "M": "c_date",
  "N": "final_date"
}
my_val = ['c_date', 'final_date', 'my_due_date', 'start_date']

所以,从我想要得到的my_val["M", "N"]

我尝试使用下划线invert来反转我的对象以获取...一切都很顺利,除了返回键而不是值..

这是我试过的,



my_obj = {
  "A": "a_id",
  "B": "b_id",
  "C": "c_id",
  "D": "d_id",
  "E": "status",
  "F": "start_time",
  "G": "end_time",
  "H": "count",
  "I": "task_desc",
  "J": "approved",
  "K": "point",
  "L": "complex",
  "M": "c_date",
  "N": "final_date"
}
my_val = ['c_date', 'final_date', 'my_due_date', 'start_date']
out = _.filter(my_val, function(v) { return _.invert(my_obj)[v]})
console.log(out)

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;

_.invert(my_obj)[v]  - this gives me a `key` but inside filter didnt..

我的代码有什么问题?怎么弄明白?

5 个答案:

答案 0 :(得分:5)

您可以使用Object.keys获取一系列密钥,并根据每个密钥的相应值是否包含在my_val数组中进行过滤; Array.prototype.includes() 确定数组是否包含某个元素并返回一个适合filter的布尔值:

&#13;
&#13;
my_obj = {
  "A": "a_id",
  "B": "b_id",
  "C": "c_id",
  "D": "d_id",
  "E": "status",
  "F": "start_time",
  "G": "end_time",
  "H": "count",
  "I": "task_desc",
  "J": "approved",
  "K": "point",
  "L": "complex",
  "M": "c_date",
  "N": "final_date"
}
my_val = ['c_date', 'final_date', 'my_due_date', 'start_date']

console.log(
  Object.keys(my_obj).filter(k => my_val.includes(my_obj[k]))
)
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这应该对你有用

&#13;
&#13;
my_obj = {
      "A": "a_id",
      "B": "b_id",
      "C": "c_id",
      "D": "d_id",
      "E": "status",
      "F": "start_time",
      "G": "end_time",
      "H": "count",
      "I": "task_desc",
      "J": "approved",
      "K": "point",
      "L": "complex",
      "M": "c_date",
      "N": "final_date"
    }

my_val = ['c_date', 'final_date', 'my_due_date', 'start_date']

var output = [];

for (var i = 0; i < my_val.length; i++) {
  for (var key in my_obj) {
    if (my_obj[key] === my_val[i]) {
      output.push(key);
    }
  }
}

console.log(output);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

如果您只想要一个键的子集:

let subset = Object.keys(my_obj).filter((key, _) =>
  my_val.includes(my_obj[key])
);

答案 3 :(得分:1)

my_obj = {
  "A": "a_id",
  "B": "b_id",
  "C": "c_id",
  "D": "d_id",
  "E": "status",
  "F": "start_time",
  "G": "end_time",
  "H": "count",
  "I": "task_desc",
  "J": "approved",
  "K": "point",
  "L": "complex",
  "M": "c_date",
  "N": "final_date"
}
let my_val = ['c_date', 'final_date', 'my_due_date', 'start_date']
let result = [];

for (let key in my_obj)
{
  for (let i = 0; i < my_val.length; i++)
  {
    if (my_obj[key] == my_val[i])
    {
      result.push(key);
    }
  }

}

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

答案 4 :(得分:0)

您未正确使用filter()。该方法需要回调函数返回一个布尔值,但在这里返回一个字符串。如果您想为my_val中的每个条目返回字符串(键),则应使用map()代替。