【问题标题】:Compare 3 arrray and find combination in javascript比较 3 个数组并在 javascript 中找到组合
【发布时间】:2021-11-25 14:54:39
【问题描述】:

我有 3 个数组如下

A: [
{
  "label": "100Watts",
  "value": "100Watts",
},
{
  "label": "135Watts",
  "value": "135Watts",
}  ]


B: [
{
"label": "Large|100cm",
"value": "Large|100cm"
},
{
"label": "Small|125mm",
"value": "Small|125mm"
}
]


C: [
{
  "label": "Black",
  "value": "Black",
},
{
  "label": "Black",
  "value": "Black",
}
]

我有一个如下的主阵列

 result:[
  { 
    "x_powerConsumption": "100Watts",
    "x_size": "Small|125mm",
    "x_color": "Black",
  },
  {
    "x_powerConsumption": "135Watts",
    "x_size": "Large|100cm",
    "x_color": "Black",
  }
  ] 

我的代码如下

let newArray = A.filter(o1 => result.some(o2 => o1.label === o2.x_size));
let newArray2 = B.filter(o1 => result.some(o2 => o1.label === o2.x_powerConsumption));
                this.setState({selectedVarientOne: newArray[0].label, selectedVarientTwo:newArray2[0].label})

但我的要求是我需要像下面这样的组合

输出:黑色 --> 100Watts --> Small|125mm

但是根据我上面的代码,它就像 黑色 --> 100Watts --> 大号|100mm

谁能告诉我如何解决这个问题?

【问题讨论】:

  • 您的问题需要进一步澄清
  • @ABDULLOKHMUKHAMMADJONOB :如果我选择黑色,则将选择 100 瓦,但黑色 + 100 瓦我需要 Small|125mm 作为尺寸变量,但根据我的代码,我得到的尺寸值 Large|100cm 这是错误的
  • 你能描述必要的属性并删除不必要的属性吗?数据如何相关?
  • @ThomasSablik 我编辑结果数组,从 JSON 中删除未使用的数据
  • 为什么你有 2 个过滤器但有 3 个数组?过滤器返回一个数组。 newArray 和 A 包含相同的元素,newArray2 和 B 包含相同的元素。你如何选择100Watts?

标签: javascript arrays react-native arraylist arrayobject


【解决方案1】:

好的,我假设您希望将对象数组与主对象匹配

我只是使用 for 循环的嵌套来查看数组(A、B 或 C)中的 values 是否与主对象(结果)中的 values 匹配 仅来自中的重要键当然是结果对象

var A=[{"label":"100Watts","value":"100Watts"},{"label":"135Watts","value":"135Watts"}]
var B=[{"label":"Large|100cm","value":"Large|100cm"},{"label":"Small|125mm","value":"Small|125mm"}]


//now for function to correlate data(sry for late answer was afk lol)
function attribute(arr,result){
  const necessary_keys=["x_powerConsumption","x_size","x_color"]
  var toReturn=[] //array with output to return
  for(let i=0;i<arr.length;i++){
    var arrValues=Object.values(arr[i]) //eg: A[0] would be ["100Watts","100Watts"]
    
    for(let j=0;j<result.length;j++){
      var resultValues=Object.keys(result[j])
      .filter(key=>necessary_keys.includes(key))
      .map(key=>result[j][key]) //result[0] would be ["100Watts","Small|125mm","Black"]
      
      if(resultValues.some(value=>arrValues.includes(value))){
        toReturn[i]=resultValues //["100Watts","Small|125mm","Black"] for arr[0]
      }
      
    }
    
  }
  return toReturn //all indexes with undefined dont correlate with result in any of its values
}


//now to use the function
let newArray=attribute(A,result)
let newArray2=attribute(B,result)

console.log("values from A:",newArray)
console.log("values from B:",newArray2)
<script>
var result=[
  {
 "dynamicPropertyMapLong": {
  "sku-Equipment_x_color": 1,
  "sku-Equipment_x_size": 1,
  "sku-Equipment_x_powerConsumption": 3
 },
"x_UAE_installationPrice": 0,
"bundleLinks": [],
"largeImage": null,
"smallImage": null,
"listVolumePrice": null,
"onlineOnly": false,
"listPrices": {
  "aed": 100,
  "loyaltyProgram": null
},
"configurationMetadata": [],
"largeImageURLs": [],
"x_skuCreationDate": null,
"productLine": null,
"listVolumePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"derivedSalePriceFrom": "aed",
"model": null,
"x_powerConsumption": "100Watts",
"barcode": null,
"salePriceEndDate": null,
"images": [],
"unitOfMeasure": null,
"primaryMediumImageURL": null,
"dynamicPropertyMapBigString": {},
"active": true,
"x_promotionDetails": "5 Percent Off: 5OFF5",
"thumbImageURLs": [],
"mediumImageURLs": [],
"primarySourceImageURL": null,
"x_description": null,
"sourceImageURLs": [],
"primarySmallImageURL": null,
"x_autoShipPrice": 0,
"productFamily": null,
"primaryThumbImageURL": null,
"nonreturnable": false,
"x_loyaltyLevel": "LEVEL2",
"displayName": "Three variant sku1",
"salePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"primaryFullImageURL": null,
"productListingSku": null,
"primaryLargeImageURL": null,
"derivedOnlineOnly": false,
"smallImageURLs": [],
"thumbnailImage": null,
"saleVolumePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"x_size": "Small|125mm",
"saleVolumePrice": null,
"salePriceStartDate": null,
"quantity": null,
"salePrice": null,
"fullImageURLs": [],
"x_isonSale": "Y",
"variantValuesOrder": {},
"x_color": "Black",
"soldAsPackage": true,
"listingSKUId": null,
"x_SAR_installationPrice": 0,
"repositoryId": "300003-1",
"derivedListPriceFrom": "aed",
"x_installationPrice": 0,
"x_costPrice": null,
"configurable": false,
"listPrice": 100
 },
{
"dynamicPropertyMapLong": {
  "sku-Equipment_x_color": 1,
  "sku-Equipment_x_size": 2,
  "sku-Equipment_x_powerConsumption": 1
},
"x_UAE_installationPrice": 0,
"bundleLinks": [],
"largeImage": null,
"smallImage": null,
"listVolumePrice": null,
"onlineOnly": false,
"listPrices": {
  "aed": 135,
  "loyaltyProgram": null
},
"configurationMetadata": [],
"largeImageURLs": [],
"x_skuCreationDate": null,
"productLine": null,
"listVolumePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"derivedSalePriceFrom": "aed",
"model": null,
"x_powerConsumption": "135Watts",
"barcode": null,
"salePriceEndDate": null,
"images": [],
"unitOfMeasure": null,
"primaryMediumImageURL": null,
"dynamicPropertyMapBigString": {},
"active": true,
"x_promotionDetails": "5 Percent Off: 5OFF5",
"thumbImageURLs": [],
"mediumImageURLs": [],
"primarySourceImageURL": null,
"x_description": null,
"sourceImageURLs": [],
"primarySmallImageURL": null,
"x_autoShipPrice": 0,
"productFamily": null,
"primaryThumbImageURL": null,
"nonreturnable": false,
"x_loyaltyLevel": "LEVEL2",
"displayName": "sku3",
"salePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"primaryFullImageURL": null,
"productListingSku": null,
"primaryLargeImageURL": null,
"derivedOnlineOnly": false,
"smallImageURLs": [],
"thumbnailImage": null,
"saleVolumePrices": {
  "aed": null,
  "loyaltyProgram": null
},
"x_size": "Large|100cm",
"saleVolumePrice": null,
"salePriceStartDate": null,
"quantity": null,
"salePrice": null,
"fullImageURLs": [],
"x_isonSale": "Y",
"variantValuesOrder": {},
"x_color": "Black",
"soldAsPackage": true,
"listingSKUId": null,
"x_SAR_installationPrice": 0,
"repositoryId": "300003-3",
"derivedListPriceFrom": "aed",
"x_installationPrice": 0,
"x_costPrice": null,
"configurable": false,
"listPrice": 135
}
]
</script>

【讨论】:

    【解决方案2】:

    假设另一个答案是正确的,这里是一个较短的版本:

    result.map(({x_powerConsumption, x_size, x_color}) => ([
      A.find(el => el.label === x_powerConsumption).value,
      B.find(el => el.label === x_size).value,
      C.find(el => el.label === x_color).value
    ]))
    

    例子:

    const A = [{ "label": "100Watts", "value": "100Watts" }, { "label": "135Watts", "value": "135Watts" }];
    const B = [{ "label": "Large|100cm", "value": "Large|100cm" }, { "label": "Small|125mm", "value": "Small|125mm" }];
    const C = [{ "label": "Black", "value": "Black" }, { "label": "Black", "value": "Black" }];
    
    const result = [
      { "x_powerConsumption": "100Watts", "x_size": "Small|125mm", "x_color": "Black" },
      { "x_powerConsumption": "135Watts", "x_size": "Large|100cm", "x_color": "Black" }
    ];
    
    console.log(result.map(({x_powerConsumption, x_size, x_color}) => ([
      A.find(el => el.label === x_powerConsumption).value,
      B.find(el => el.label === x_size).value,
      C.find(el => el.label === x_color).value
    ])));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多