我有一个数组:

this.questions = [
      {id: 1, question: "Do you feel a connection to a higher source and have a sense of comfort knowing that you are part of something greater than yourself?", category: "Spiritual", subs: []},
      {id: 2, question: "Do you feel you are free of unhealthy behavior that impacts your overall well-being?", category: "Habits", subs: []},
      {id: 3, question: "Do you feel you have healthy and fulfilling relationships?", category: "Relationships", subs: []},
      {id: 4, question: "Do you feel you have a sense of purpose and that you have a positive outlook about yourself and life?", category: "Emotional Well-being", subs: []},
      {id: 5, question: "Do you feel you have a healthy diet and that you are fueling your body for optimal health? ", category: "Eating Habits ", subs: []},
      {id: 6, question: "Do you feel that you get enough rest and that your stress level is healthy?", category: "Relaxation ", subs: []},
      {id: 7, question: "Do you feel you get enough physical activity for optimal health?", category: "Exercise ", subs: []},
      {id: 8, question: "Do you feel you practice self-care and go to the doctor regularly?", category: "Medical Maintenance", subs: []},
      {id: 9, question: "Do you feel satisfied with your income and economic stability?", category: "Financial", subs: []},
      {id: 10, question: "Do you feel you do fun things and laugh enough in your life?", category: "Play", subs: []},
      {id: 11, question: "Do you feel you have a healthy sense of balance in this area of your life?", category: "Work-life Balance", subs: []},
      {id: 12, question: "Do you feel a sense of peace and contentment  in your home? ", category: "Home Environment", subs: []},
      {id: 13, question: "Do you feel that you are challenged and growing as a person?", category: "Intellectual Wellbeing", subs: []},
      {id: 14, question: "Do you feel content with what you see when you look in the mirror?", category: "Self-image", subs: []},
      {id: 15, question: "Do you feel engaged at work and a sense of fulfillment with your job?", category: "Work Satisfaction", subs: []}

    ];

I have a function:

getDimensions(id) {    
   //need logic to get object from questions array based on ID.      
}

How can I write a map to get the correct object form the array based on ID?

推荐答案

你可以用.filter().find().一个区别是filter将迭代所有项,并返回作为数组传递条件的任何项,而find将返回第一个匹配项并中断迭代.

实例

var questions = [
      {id: 1, question: "Do you feel a connection to a higher source and have a sense of comfort knowing that you are part of something greater than yourself?", category: "Spiritual", subs: []},
      {id: 2, question: "Do you feel you are free of unhealthy behavior that impacts your overall well-being?", category: "Habits", subs: []},
      {id: 3, question: "Do you feel you have healthy and fulfilling relationships?", category: "Relationships", subs: []},
      {id: 4, question: "Do you feel you have a sense of purpose and that you have a positive outlook about yourself and life?", category: "Emotional Well-being", subs: []},
      {id: 5, question: "Do you feel you have a healthy diet and that you are fueling your body for optimal health? ", category: "Eating Habits ", subs: []},
      {id: 6, question: "Do you feel that you get enough rest and that your stress level is healthy?", category: "Relaxation ", subs: []},
      {id: 7, question: "Do you feel you get enough physical activity for optimal health?", category: "Exercise ", subs: []},
      {id: 8, question: "Do you feel you practice self-care and go to the doctor regularly?", category: "Medical Maintenance", subs: []},
      {id: 9, question: "Do you feel satisfied with your income and economic stability?", category: "Financial", subs: []},
      {id: 10, question: "Do you feel you do fun things and laugh enough in your life?", category: "Play", subs: []},
      {id: 11, question: "Do you feel you have a healthy sense of balance in this area of your life?", category: "Work-life Balance", subs: []},
      {id: 12, question: "Do you feel a sense of peace and contentment  in your home? ", category: "Home Environment", subs: []},
      {id: 13, question: "Do you feel that you are challenged and growing as a person?", category: "Intellectual Wellbeing", subs: []},
      {id: 14, question: "Do you feel content with what you see when you look in the mirror?", category: "Self-image", subs: []},
      {id: 15, question: "Do you feel engaged at work and a sense of fulfillment with your job?", category: "Work Satisfaction", subs: []}
];

function getDimensionsByFilter(id){
  return questions.filter(x => x.id === id);
}

function getDimensionsByFind(id){
  return questions.find(x => x.id === id);
}

var test = getDimensionsByFilter(10);
console.log(test);

test = getDimensionsByFind(10);
console.log(test);

Json相关问答推荐

jq不会为空输入返回非零

Swift解码错误类型与`Bool`type不一致

如何使用Laravel在MariaDB JSON kolumn中使用unicode字符

抓取低于w jolt的对象级别

Jolt变换将字段移动到数组的每个元素中

在Reaction中从JSON文件中筛选数组

序列化从/到空值

nlohmann json:为什么我会得到一个解析错误?

使用 jq,如何将两个属性构成键的对象数组转换为对象的索引对象?

如何通过 jolt 将一个对象中的键和值添加到数组中的每个对象中

如何从字符串中创建一个逗号分隔的列表,由 API 中的 JSON 对象内的编号空格分隔?

JQ 中的样本标准偏差

jq搜索特定字符串并输出对应的父值

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

如何使用 LINQ 在 C# 中重构对象?

在 JSON API Wordpress 上启用 CORS

什么是类型和类型令牌?

Spring MVC控制器中的JSON参数

嵌套 JSON:如何向对象添加(推送)新项目?

如何在dart Flutter 中将json字符串转换为json对象?