gpt4 book ai didi

javascript - 检查对象是否有key和value

转载 作者:行者123 更新时间:2023-12-03 02:19:18 25 4
gpt4 key购买 nike

类(class)对象:

data.courses: [{name: 'JAVA', categoryid: '1'},
{name: 'PHP', categoryid: '1'},
{name: 'HR', categoryid: '2'}]

类别对象:

data.categories: [{catname: 'Information technology',categoryid: '1'},
{catname: 'HR',categoryid: '2'},
{catname: 'Sales',categoryid: '3'}]

我的预期结果:

finalObjCategories对象:

 data.finalObjCategories: [{catname: 'Information technology',categoryid: '1'},
{catname: 'HR',categoryid: '2'}]

//{catname: 'Sales',categoryid: '3'} 已删除,因为类(class)对象中不存在此类别的类(class)。

我只想获取那些类别有类(class)数据的类别记录,我该怎么做?

while(grCategory.next())  {
var categoryName=grCategory.getValue('name');
var categoryId=grCategory.getUniqueValue();
//here condition needed(couse cat lenght > 1 push else dontpush)
//if (data.courses.hasOwnProperty('name');){
data.categories.push({name: categoryName, id: categoryId});
//}
}

最佳答案

你的问题真的很不清楚,但如果我理解它,你可以使用 .map.find 来得到你想要的。

var courses =  [
{name: 'JAVA',categoryid: '1'},
{name: 'PHP',categoryid: '1'},
{name: 'HR',categoryid: '2'}
]

var categories = [
{catname: 'Information technology',categoryid: '1'},
{catname: 'HR',categoryid: '2'},
{catname: 'Sales',categoryid: '3'}
]

var result = [];
courses.map(function(course){
var crs = categories.find(function(cat){return course.categoryid == cat.categoryid})

if(typeof crs !== "undefined" && result.indexOf(crs) < 0){
result.push(crs);
}
})

console.log(result)

Here is the fiddle.

关于javascript - 检查对象是否有key和value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49235842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com