Javascript 检查并动态推送新数组中的元素
Javascript check and push dynamically elements in a new array
我有一个像下面数据这样的元素列表
[
{
"title": "First",
"catalogCategories": [ "sport", "economy" ]
},
{
"title": "Second",
"catalogCategories": [ "politics", "tourism" ]
},
{
"title": "Third",
"catalogCategories": [ "sport", "universe" ]
},
{
"title": "Fourth",
"catalogCategories": [ "economy", "politics" ]
}
]
我正在检查列表中的每个元素是否存在 catalogCategories
数组,如果存在,我只将任何元素的类别推入 catalogData
数组一次,因为碰巧任何元素的类别都可以相同,我在 catalogData
数组中过滤 catalogCategories
中元素的所有类别
这是下面的代码
let categoryData = [];
let isCategory = false
for (let product of list) {
if (product.catalogCategories[0].length > 0) {
isCategory = true
let isFound = false
for (let i = 0; i < categoryData.length; i++) {
if (categoryData[i].value === product.catalogCategories[0] ||
categoryData[i].value === product.catalogCategories[1] ) {
isFound = true;
}
}
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
}
我的问题就在这里
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
如您所见,在所有这些检查之后,我总是只推送 catalogCategories
的第一个元素,我如何动态写下我可以推送第一个和/或第二个元素?
"value": product.catalogCategories[0]
"label": product.catalogCategories[0]
您需要首先使用 map
检查整个 catalogCategories
而不仅仅是第一个元素 product.catalogCategories[0]
并使用 new Map
来跟踪不重复的类别。
const list = [
{
"title": "First",
"catalogCategories": [ "sport", "economy" ]
},
{
"title": "Second",
"catalogCategories": [ "politics", "tourism" ]
},
{
"title": "Third",
"catalogCategories": [ "sport", "universe" ]
},
{
"title": "Fourth",
"catalogCategories": [ "economy", "politics" ]
}
]
let categoryDataMap = new Map();
for (let product of list) {
if (product.catalogCategories.length > 0) {
product.catalogCategories.map(catalog=>{
if(!categoryDataMap.has(catalog)){
categoryDataMap.set(catalog,{"name": "catalogCategories", "label": catalog, "selected": false, "value": catalog})
}
})
}
}
const categoryData = Array.from(categoryDataMap.values())
console.log(categoryData)
我有一个像下面数据这样的元素列表
[
{
"title": "First",
"catalogCategories": [ "sport", "economy" ]
},
{
"title": "Second",
"catalogCategories": [ "politics", "tourism" ]
},
{
"title": "Third",
"catalogCategories": [ "sport", "universe" ]
},
{
"title": "Fourth",
"catalogCategories": [ "economy", "politics" ]
}
]
我正在检查列表中的每个元素是否存在 catalogCategories
数组,如果存在,我只将任何元素的类别推入 catalogData
数组一次,因为碰巧任何元素的类别都可以相同,我在 catalogData
数组中过滤 catalogCategories
这是下面的代码
let categoryData = [];
let isCategory = false
for (let product of list) {
if (product.catalogCategories[0].length > 0) {
isCategory = true
let isFound = false
for (let i = 0; i < categoryData.length; i++) {
if (categoryData[i].value === product.catalogCategories[0] ||
categoryData[i].value === product.catalogCategories[1] ) {
isFound = true;
}
}
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
}
我的问题就在这里
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
如您所见,在所有这些检查之后,我总是只推送 catalogCategories
的第一个元素,我如何动态写下我可以推送第一个和/或第二个元素?
"value": product.catalogCategories[0]
"label": product.catalogCategories[0]
您需要首先使用 map
检查整个 catalogCategories
而不仅仅是第一个元素 product.catalogCategories[0]
并使用 new Map
来跟踪不重复的类别。
const list = [
{
"title": "First",
"catalogCategories": [ "sport", "economy" ]
},
{
"title": "Second",
"catalogCategories": [ "politics", "tourism" ]
},
{
"title": "Third",
"catalogCategories": [ "sport", "universe" ]
},
{
"title": "Fourth",
"catalogCategories": [ "economy", "politics" ]
}
]
let categoryDataMap = new Map();
for (let product of list) {
if (product.catalogCategories.length > 0) {
product.catalogCategories.map(catalog=>{
if(!categoryDataMap.has(catalog)){
categoryDataMap.set(catalog,{"name": "catalogCategories", "label": catalog, "selected": false, "value": catalog})
}
})
}
}
const categoryData = Array.from(categoryDataMap.values())
console.log(categoryData)