根据条件获取数组中特定字段的值

Get the value of specific fields in an array based on a condition

我下面有一个数组。 对于此数组中每个对象元素中的每个 "_id",都有一个名为 "form".

的数组

我想得到的是每个 _idanswerValue,其中 form 中的 field 值是 cState.

let array = [{
"_id" : "a1",
"user" : "John",
"form" : [ 
    {
        "question" : "question1",
        "questionValue" : "Which state do you live in?",
        "answers" : [ 
            "answer1"
        ],
        "answerValue" : [ 
            "Arizona"
        ],
        "field" : "cState",
    }, 
    {
        "question" : "question2",
        "questionValue" : "What is your nationality?",
        "answers" : [ 
            "answer2"
        ],
        "answerValue" : [ 
            "American"
        ],
        "field" : "nationality",
    }, 
"_id" : "a2",
"user" : "Mike",
"form" : [ 
    {
        "question" : "question1",
        "questionValue" : "Which state do you live in?",
        "answers" : [ 
            "answer3"
        ],
        "answerValue" : [ 
            "Florida"
        ],
        "field" : "cState",
    }, 
    {
        "question" : "question2",
        "questionValue" : "What is your nationality?",
        "answers" : [ 
            "answer2"
        ],
        "answerValue" : [ 
            "American"
        ],
        "field" : "nationality",
    },
}]

预期输出

[{
  "_id" : "a1",
  "user" : "John",
  "answerValue": "Arizona"
  },
 {
  "_id" : "a2",
  "user" : "Mike",
  "answerValue": "Florida"
  },
]

这是我尝试过的:

let allDemographics
  allDemographics = array.map((item) => {

    return {  
      user: item.array._id,
      nationality: item.array.nationality,
      state: item.array.form,
    }
  })

试一试:

let array = [
  {
    "_id" : "a1",
    "user" : "John",
    "form" : [ 
        {
            "question" : "question1",
            "questionValue" : "Which state do you live in?",
            "answers" : [ 
                "answer1"
            ],
            "answerValue" : [ 
                "Arizona"
            ],
            "field" : "cState",
        }, 
        {
            "question" : "question2",
            "questionValue" : "What is your nationality?",
            "answers" : [ 
                "answer2"
            ],
            "answerValue" : [ 
                "American"
            ],
            "field" : "nationality",
        }
     ]
  }, 
  {
    "_id" : "a2",
    "user" : "Mike",
    "form" : [ 
        {
            "question" : "question1",
            "questionValue" : "Which state do you live in?",
            "answers" : [ 
                "answer3"
            ],
            "answerValue" : [ 
                "Florida"
            ],
            "field" : "cState",
        }, 
        {
            "question" : "question2",
            "questionValue" : "What is your nationality?",
            "answers" : [ 
                "answer2"
            ],
            "answerValue" : [ 
                "American"
            ],
            "field" : "nationality",
        },
    ]
  }
]
let allDemographics = array.map((item) => {
  let fieldCstate = item.form.find(form => form.field === "cState")
  return {  
    _id: item._id,
    user: item.user,
    answerValue: fieldCstate.answerValue[0],
  }
})
console.log(allDemographics)

我喜欢这个,因为你在映射之前进行了过滤。如果你有一个大数组,这可能会给你更好的性能:

let arr = array.filter(x => x.form.every(y => y.field === "cState"));
const data = arr.map((x) => ({ user: x.user, id: x._id, 
         answer:x.form[0].answerValue }));
    
console.log(data[0].answer)

或者一个班轮:

let arr = array.filter(x => x.form.every(y => y.field === "cState"))
 .map(x => ({ user: x.user, id: x._id, answer:x.form[0].answerValue })   
 )