2016-08-18 22 views
14

Tôi có một mảng:góc đối tượng get từ mảng bởi Id

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: []} 

    ]; 

Tôi có một chức năng:

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

Làm thế nào tôi có thể viết một bản đồ để có được đúng đối tượng hình thành các mảng dựa trên ID?

+5

'kết quả var = this.questions.find (o => o.id === 10); ' – Tushar

Trả lời

39

Bạn có thể sử dụng .filter() hoặc .find(). Một sự khác biệt là filter sẽ lặp lại trên tất cả các mục và trả về bất kỳ mục nào vượt qua điều kiện dưới dạng mảng trong khi find sẽ trả lại mục phù hợp đầu tiên và ngắt lặp lại.

Ví dụ

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);

+1

@JudsonTerrell: Nếu câu trả lời đã giúp bạn, tốt hơn là đánh dấu nó là chấp nhận, nó cung cấp cho bạn điểm, cung cấp cho người trả lời điểm, giúp mọi người với cùng một vấn đề tìm câu trả lời :) – DrakaSAN

3
getDimensions(id) { 
    var obj = questions.filter(function(node) { 
     return node.id==id; 
    }); 

    return obj; 
} 
+0

LRI Loại lỗi: Không thể đọc thuộc tính 'tìm' không xác định –

Các vấn đề liên quan