簡體   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