假设我们在JavaScript中具有以下对象-
ObjectValue =[ { "id": "101", "details": { Name:"John",subjectName:"JavaScript" }}, { "id": "102", "details": { Name:"David",subjectName:"MongoDB" }}, { "id": "103" } ]
要使用删除对象filter()
,代码如下:
ObjectValue =[ { "id": "101", "details": { Name:"John",subjectName:"JavaScript" }}, { "id": "102", "details": { Name:"David",subjectName:"MongoDB" }}, { "id": "103" } ] output=ObjectValue.filter(obj=>obj.details) console.log(output)
要运行以上程序,您需要使用以下命令-
node fileName.js.
输出结果
这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo46.js [ { id: '101', details: { Name: 'John', subjectName: 'JavaScript' } }, { id: '102', details: { Name: 'David', subjectName: 'MongoDB' } } ]