【问题标题】:Typescript/Javascript for loop on array, push value of a key to new array as key with the value 1 - if already exists, incrementTypescript/Javascript for 在数组上循环,将键的值作为值为 1 的键推送到新数组 - 如果已经存在,则递增
【发布时间】:2019-01-21 14:14:13
【问题描述】:

目的是拥有一个数组,其中包含来自另一个数组的特定键的所有唯一值。这些唯一值(现在是新数组中的键)将具有它们在原始数组中出现的次数作为值。

这是我目前的代码:

for (let i = 0; i < data.length; i++) {


           let theStupidKey = data[i].DeliveryStatus;

           if (
             this.differentValuesOfStatus.indexOf(theStupidKey) == "-1"
           ) {
             this.differentValuesOfStatus.push(theStupidKey: 1);
             // this.differentValuesOfStatus[theStupidKey].push(theStupidKey = 1);
           }
           else {
             this.differentValuesOfStatus[theStupidKey] = 1
           }
        }
        console.log(this.differentValuesOfStatus);
      };

但是语法是错误的,我尝试了所有我能想到的方法来让它工作。

基本上我是在遍历一个数组。

如果第二个数组中不存在“deliverystatus”键的值,我将其添加为“1”。

如果它已经存在,我想添加另一个数字。

【问题讨论】:

    标签: javascript arrays json angular typescript


    【解决方案1】:

    一个数组只能保存一个值列表。如果你想存储键和对应的值,你应该使用映射:

    class Test {
      differentValuesOfStatus: Map<string, number>;
    
      test(data: {DeliveryStatus: string}[]) {
        this.differentValuesOfStatus = new Map<string, number>();
        for (let i = 0; i < data.length; i++) {
          let theStupidKey = data[i].DeliveryStatus;
    
          let value = this.differentValuesOfStatus.get(theStupidKey);
          this.differentValuesOfStatus.set(theStupidKey,
            (value === undefined ? 0 : value) + 1);
        }
        console.log(this.differentValuesOfStatus);
      }
    }
    
    let t = new Test();
    t.test([
      { DeliveryStatus: "Accepted" },
      { DeliveryStatus: "Rejected" },
      { DeliveryStatus: "Accepted" }
    ]);
    

    【讨论】:

      【解决方案2】:

      我认为您可以创建一个对象,将您的状态名称作为键和出现次数作为值。这是实现此目的的代码。

      for (let i = 0; i < data.length; i++)
      {
          let theStupidKey = data[i].DeliveryStatus;
      
          if (!this.differentValuesOfStatus.hasOwnProperty(theStupidKey)) {
              this.differentValuesOfStatus[theStupidKey] = 0;
          }
          this.differentValuesOfStatus[theStupidKey] = this.differentValuesOfStatus[theStupidKey] + 1;
      }
      console.log(this.differentValuesOfStatus);
      

      你会得到这样的回应。

      {Status1: 2, Status2: 3, Status3: 1, Status4: 3}
      

      但如果你想要一个数组,你可以这样做,

      let newArray = [];
      for (var key in this.differentValuesOfStatus) {
          newArray.push({ "Status": key, "Occurence": this.differentValuesOfStatus[key] });
      }
      console.log(newArray);
      

      输出将是

      [{status: "Status1", Occurence: 2},
      {status: "Status2", Occurence: 3},
      {status: "Status3", Occurence: 1},
      {status: "Status4", Occurence: 3}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 2020-12-10
        • 2014-12-07
        • 2019-09-15
        • 2019-05-04
        相关资源
        最近更新 更多