迭代json并找到特定的键

时间:2018-04-21 21:24:04

标签: javascript jquery html angularjs typescript

  • 我是js的新手。
  • 我正在尝试迭代json结构。
  • 当我看到isDefault:true时,我需要显示为默认名称。
  • 但是现在我正在编写我的js代码中的值。
  • 你能告诉我如何解决它。
  • 在下面提供我的代码。
data.downlinkmoons =[{Lion: 237, birds: "Animal Sports Bay Area", fish: 1, isDefault: true, wire: ""},
                        {Lion: 238, birds: "Animal Sports California", fish: 1, wire: ""},
                        {Lion: 239, birds: "Animal Sports Washington", fish: 1, wire: ""},
                        {Lion: 240, birds: "Animal Sports Philadelphia", fish: 1, wire: ""}]


playOutside(data: any) {
        let that = this;
        let tempObj = {};
        //tempObj['Lion'] = 237;
        tempObj['Lion'] = data.downlinkmoons[0].Lion;
        tempObj['birds'] = 'Animal Sports Bay Area';
        // tempObj['Lion'] = data.selectedLion;
        // tempObj['birds'] = data.selectedbirds;
        this.moonTempArray = [];
        this.moonTempArray.push(tempObj);
        let moonsdata = data.downlinkmoons;
        let moonsDataList = this.moonTempArray;
        let selectedmoon = moonsdata.find(elem => elem.Lion == 237);
        this.downlinkBulkUpdateVal.Lion = 237;
        // let selectedmoon = moonsdata.find(elem => elem.Lion == data.selectedLion);
        // this.bulkCreateVal.Lion = data.selectedLion;
        this.selectedmoonArr = selectedmoon;
        this.moonCarousel.setmoonData(moonsdata, moonsDataList, 237);
        //this.moonCarousel.setmoonData(moonsdata, moonsDataList, data.selectedLion);
        $("#moonCarouselLabel .moonHint").css("display", "none");
        setTimeout(function () {
            $("#unSelectedmoonsLogoBox1 .currentNwLogo").bind("click", function (e) {
                e.stopPropagation();
                that.singleSelection(data, e);
            });
        }, 100);
    }

2 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.find()功能:

var downlinkmoons =[{Lion: 237, birds: "Animal Sports Bay Area", fish: 1, isDefault: true, wire: ""},
                        {Lion: 238, birds: "Animal Sports California", fish: 1, wire: ""},
                        {Lion: 239, birds: "Animal Sports Washington", fish: 1, wire: ""},
                        {Lion: 240, birds: "Animal Sports Philadelphia", fish: 1, wire: ""}];

function getDefault(downlinkmoons){                                                
    return downlinkmoons.find((elem) => { return elem.isDefault; });                 
}

console.log(getDefault(downlinkmoons))

有关Array.prototype.find的更多信息,请here

答案 1 :(得分:0)

这是

var downlinkmoons =[{Lion: 237, birds: "Animal Sports Bay Area", fish: 1, isDefault: true, wire: ""},
                        {Lion: 238, birds: "Animal Sports California", fish: 1, wire: ""},
                        {Lion: 239, birds: "Animal Sports Washington", fish: 1, wire: ""},
                        {Lion: 240, birds: "Animal Sports Philadelphia", fish: 1, wire: ""}];
function getDefault(downlinkmoons){                                                
for(var i=0; i<downlinkmoons.length; i++){
     if(downlinkmoons[i].hasOwnProperty("isDefault"))
     { 
          if(downlinkmoons[i].isDefault)
             return downlinkmoons[i];
            
          break;
     }
     
}

}
console.log(getDefault(downlinkmoons))

在脚本中使用它请执行以下操作

tempObj['Lion'] = getDefault(downlinkmoons).Lion