假设以下是我们的对象数组-
var details = [{ studentName: "John", studentMarks: 92 }, { studentName: "David", studentMarks: 89 }, { studentName: "Mike", studentMarks: 98 }, ];
要仅获取JavaScript对象数组中的特定值,请使用的概念filter()
。
var details = [{ studentName: "John", studentMarks: 92 }, { studentName: "David", studentMarks: 89 }, { studentName: "Mike", studentMarks: 98 }, ]; var specificValuesFromArray = details.filter(obj => obj.studentMarks === 92 || obj.studentMarks === 98); console.log(specificValuesFromArray)
要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo177.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo177.js [ { studentName: 'John', studentMarks: 92 }, { studentName: 'Mike', studentMarks: 98 } ]