You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断newField中new_field_name值是否存在于mapping的value属性中

检查newField字段是否存在于mapping中的实现方法

需求:验证newField数组内每个对象的new_field_name属性值,是否存在于mapping数组对象的value属性集合中。

方法一:用Set优化查找性能(推荐大数据量场景)

先把mapping中的所有value提取存入Set(Set的查找时间复杂度为O(1),比数组遍历更高效),再遍历newField做存在性检查:

let mapping =  [ { "value": "title", "text": "Do Not Import", "custom": null }, { "value": "short_message", "text": "Do Not Import", "custom": null }, { "value": "url", "text": "Do Not Import", "custom": null }, { "value": "campaign_id", "text": "Do Not Import", "custom": null }, { "value": "aff_code", "text": "Do Not Import", "custom": null }, { "value": "campaign_url", "text": "Do Not Import", "custom": null }, { "value": "other_data", "text": "Do Not Import", "custom": null }, { "value": "product_id", "text": "Do Not Import", "custom": null }, { "value": "created_at", "text": "Do Not Import", "custom": null }, { "value": "product_active", "text": "Do Not Import", "custom": null }, { "value": "brand", "text": "Do Not Import", "custom": null }, { "value": "old_price", "text": "Do Not Import", "custom": null }, { "value": "subcategory", "text": "Do Not Import", "custom": null }, { "value": "category", "text": "Do Not Import", "custom": null }, { "value": "price", "text": "Do Not Import", "custom": null }, { "value": "widget_name", "text": "Do Not Import", "custom": null }, { "value": "campaign_name", "text": "Do Not Import", "custom": null }, { "value": "image_urls", "text": "Do Not Import", "custom": null }, { "value": "description", "text": "Do Not Import", "custom": null } ]; 
let newField = [ { "new_field_name": "one ", "new_field_value": "" }, { "new_field_name": "url", "new_field_value": "" } ];

// 提取mapping的所有value到Set
const mappingValueSet = new Set(mapping.map(item => item.value));

// 遍历newField执行检查
newField.forEach(field => {
  // 可选:去除字段名首尾空格,避免空格导致的匹配失败
  const cleanedFieldName = field.new_field_name.trim();
  const isExists = mappingValueSet.has(cleanedFieldName);
  
  // 输出检查结果
  console.log(`字段「${field.new_field_name}」是否存在于mapping中:${isExists}`);
  
  // 可将结果附加到原对象,方便后续使用
  field.isExistsInMapping = isExists;
});

// 查看处理后的newField数组
console.log(newField);

方法二:直接遍历检查(适合小数据量场景)

如果数据量不大,可直接用Array.some()遍历mapping做匹配,代码更简洁:

let mapping =  [ { "value": "title", "text": "Do Not Import", "custom": null }, { "value": "short_message", "text": "Do Not Import", "custom": null }, { "value": "url", "text": "Do Not Import", "custom": null }, { "value": "campaign_id", "text": "Do Not Import", "custom": null }, { "value": "aff_code", "text": "Do Not Import", "custom": null }, { "value": "campaign_url", "text": "Do Not Import", "custom": null }, { "value": "other_data", "text": "Do Not Import", "custom": null }, { "value": "product_id", "text": "Do Not Import", "custom": null }, { "value": "created_at", "text": "Do Not Import", "custom": null }, { "value": "product_active", "text": "Do Not Import", "custom": null }, { "value": "brand", "text": "Do Not Import", "custom": null }, { "value": "old_price", "text": "Do Not Import", "custom": null }, { "value": "subcategory", "text": "Do Not Import", "custom": null }, { "value": "category", "text": "Do Not Import", "custom": null }, { "value": "price", "text": "Do Not Import", "custom": null }, { "value": "widget_name", "text": "Do Not Import", "custom": null }, { "value": "campaign_name", "text": "Do Not Import", "custom": null }, { "value": "image_urls", "text": "Do Not Import", "custom": null }, { "value": "description", "text": "Do Not Import", "custom": null } ]; 
let newField = [ { "new_field_name": "one ", "new_field_value": "" }, { "new_field_name": "url", "new_field_value": "" } ];

newField.forEach(field => {
  const cleanedFieldName = field.new_field_name.trim();
  const isExists = mapping.some(item => item.value === cleanedFieldName);
  
  console.log(`字段「${field.new_field_name}」是否存在于mapping中:${isExists}`);
  field.isExistsInMapping = isExists;
});

console.log(newField);

示例输出

运行上述代码后,控制台会输出:

字段「one 」是否存在于mapping中:false
字段「url」是否存在于mapping中:true

内容的提问来源于stack exchange,提问作者Shibbir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 16:36:22