【问题标题】:iterate json and find particular key迭代 json 并找到特定的键
【发布时间】:2023-03-29 13:53:01
【问题描述】:
  • 我是 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);
    }

【问题讨论】:

标签: javascript jquery html angularjs typescript


【解决方案1】:

这里是

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

【讨论】:

  • 嗨,但是如何在 playOutside 方法中动态传递 ---> tempObj['Lion'] = data.downlinkmoons[0].Lion;
  • 将其包装在一个函数中并将返回值赋给tempObj['Lion']
  • 嗨...感谢您的回复...但是如果我在 playOutside 方法中使用相同的代码,它就不起作用...您能告诉我如何传递给 playOutside 方法
【解决方案2】:

你可以使用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

【讨论】:

  • 嗨...感谢您的回复...但是如果我在 playOutside 方法中使用相同的代码,则它的 data.find 不是函数
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2020-05-15
相关资源
最近更新 更多