针对“页面 ID”为多个产品定义 JQuery 数组

Defining JQuery arrays for multiple products against 'page ids'

提问人:pathetic.aesthetic 提问时间:9/7/2023 更新时间:9/7/2023 访问量:37

问:

我们目前正在寻找创建一个数组,我们可以在其中单独定义“页面 ID”(由平台自动创建)以及它们适用于哪些产品。我需要扁平化数组,以便第一个 IF 语句正常工作,该语句检查当前页面 ID 是否与任何定义的数组匹配。

然后,我需要第二个 IF 语句来检查当前页面 ID 与各个数组索引(也称为“product1”)。如果它与该特定产品存在的数组值匹配,则显示适用于该产品的内容。

var solutions = [];
solutions["product1"] = [46, 47, 48, 49];
solutions["product2"] = [40, 41, 42, 43];
var currentID = pageData.content.category.id; //gets current page ID

$.each(solutions, function (index, val) {
 if ((pageData.content.category.id == val) > -1) {
   /* do something */
 }
 if($.inArray(currentID, solutions[index]) !== -1) {
   /* do something */
 }
});
jQuery 数组

评论

0赞 Barmar 9/7/2023
solutions应该是一个对象,而不是一个数组。数组有数字索引,对象有字符串键。

答:

1赞 Chris Barr 9/7/2023 #1

第一:这里不需要使用jQuery。

下一步:你希望 是一个对象,而不是一个数组。使用对象,您可以按键名称查找项目。solutions

最后:要检查变量是否等于对象中的键,一种方法是遍历键,然后在找到匹配项时执行某些操作,如下所示:

const pageData = {
  content: {
    category:{ id: "product2" }
  }
}
//=================================

const solutions = {
  product1: [46, 47, 48, 49],
  product2: [40, 41, 42, 43]
};

const currentID = pageData.content.category.id; //gets current page ID

Object.keys(solutions).forEach((key) => {
  const val = solutions[key];
  if (currentID === key) {
    console.log(`Current ID: ${key} and the array is: `, val)
  }
});

不过,一种更简单的方法是直接访问该对象属性,因为我们有 ID。我推荐这种方式。

const pageData = {
  content: {
    category:{ id: "product2" }
  }
}
//=================================

const solutions = {
  product1: [46, 47, 48, 49],
  product2: [40, 41, 42, 43]
};

const currentID = pageData.content.category.id; //gets current page ID
const currentProduct = solutions[currentID];

console.log(`Current ID: ${currentID} and the array is: `, currentProduct)


根据新信息进行修改

const pageData = {
  content: {
    category:{ id: 42 }
  }
}
//=================================

const solutions = {
  product1: [46, 47, 48, 49],
  product2: [40, 41, 42, 43]
};

const currentID = pageData.content.category.id; //gets current page ID

//Object.entries provides an tuple array of object keys as the first item and object values as the second item
//Using .find() we can narrow this down to only give us the one product that contains the matching ID
const match = Object.entries(solutions).find(s=>s[1].includes(currentID));

if(match){
  //If a match is found, the first item in the array will be the product name, or the key of the object
  //If needed, match[1] will give you all the items for the matching product
  console.log(`Current ID: ${currentID}, Current Product: ${match[0]}`)
  console.log(`All items for this product:`, match[1])
} else {
  //If there is no match found, handle that
  console.log(`Unable to locate a product that contains ID ${currentID}`);
}

评论

0赞 pathetic.aesthetic 9/7/2023
感谢您的帮助,但不幸的是,这并不能解决我的问题。页面的 ID 始终是一个数字,而不是产品名称,并且可以有多个数字代表一个产品。因此,例如,如果我正在查看一个页面,并且 ID 为 46,我希望代码能够识别我所在的页面是针对“product1”的。
1赞 Chris Barr 9/7/2023
好的,我认为这在您最初的问题中不是很清楚,因为没有提供当前 ID 的示例数据。无论如何,我已经更新了我的答案,如你现在所描述的那样。