繁体   English   中英

将数组值匹配到JavaScript中的对象属性

[英]Match array values to object property in JavaScript

我一直在尝试将取自查询字符串的数组值匹配到对象属性并在匹配时返回子对象时遇到问题。

我的数组:

["model1-all", "model2-all", "model3-all"] 

数组可以为空,也可以返回一个,两个或所有值。

我的对象:

[
  {
    "model1-all": [
      {
        "text": "Model1",
        "id": "model1-all",
        "image": "http://example.com/images/model1/front.png",
        "models": [
          {
            "id": "model1-all",
            "text": "All",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model1-coupe",
            "text": "Coupe",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model1-convertible",
            "text": "Convertible",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          }
        ]
      }
    ],
    "model2-all": [
      {
        "text": "Model2",
        "id": "model2-all",
        "image": "http://example.com/images/model2/front.png",
        "models": [
          {
            "id": "model2-all",
            "text": "All",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model2-hatchback",
            "text": "Hatchback",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model2-convertible",
            "text": "Convertible",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model2-estate",
            "text": "Estate",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          }
        ]
      }
    ],
    "model3-all": [
      {
        "text": "Model3",
        "id": "model3-all",
        "image": "http://example.com/images/model3/front.png",
        "models": [
          {
            "id": "model3-all",
            "text": "All",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model3-saloon",
            "text": "Saloon",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          },
          {
            "id": "model3-estate",
            "text": "Estate",
            "positiveFeatureCopy": null,
            "negativeFeatureCopy": null,
            "weight": 1000
          }
        ]
      }
    ]
  }
]

我需要实现的是,当数组中的某个值与对象之一匹配时,例如[{"model1-all" : [... ,我想返回其子对象:

    "models": [
                 {"id":"model1-all", "...":"....", "...", "...":"..."},
                 {"id":"model1-coupe", "...":"....", "...", "...":"..."},
                 {"id":"model1-convertible", "...":"....", "...", "...":"..."}
              ]

我可以使用jQuery或Underscore。

提前致谢

不需要jQuery

var models = [];
var matches = ["model1-all", "model2-all", "model3-all"];

for (var key in data[0]) {
    //No indexOf for IE8 compatibility
    if (matches.indexOf(key) > -1) {
        models.push({
            id: key,
            child: data[0][key]
        });
    }

    //Not using indexOf
    for (var i = 0; i < matches.length; i++) {
        if (matches[i] == key) {
            models.push({
                id: key,
                child: data[0][key]
            });
            break;
        }
    }
}

结果是

models = [
    {
        id: "model1-all",
        child: <object from model1-all>
    },
    ..
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM