簡體   English   中英

根據數組中 object 的索引,為數組中的每個 object 分配一個具有唯一 id 的新鍵

[英]Assign a new key to each object in the array with a unique id based on the index of the object in the array

我正在嘗試根據數組中 object 的索引為數組中的每個 object 分配一個新鍵,該鍵具有唯一 ID。

當我單擊一個按鈕時,它會運行

    this.setState({ testStatus: "running", clicked: true });
    tests.map(test => {
      test.run(x =>
        this.setState({
          testStatus: {
            ...this.state.testStatus,
            [test.id]: x ? "passed" : "failed"
          }
        })
      );
    });

每個測試都有一個隨機延遲和隨機 Boolean 結果當我 console.log -> testStatus

它記錄以下內容

0: "r"
1: "u"
2: "n"
3: "n"
4: "i"
5: "n"
6: "g"
undefined: "passed" <<<<---- variable outcome | undefined is what I want to have a unique id
**The array**
const tests = [
    { description: "Gochujang small batch live-edge green juice photo booth pinterest.",          run: testsResult() },
    { description: " DIY shaman narwhal before they sold out chambray aesthetic.",         run: testsResult() },
    { description: "Tousled cliche master cleanse, cray chicharrones fixie skateboard", run: testsResult() },
    { description: "Fanny pack keffiyeh coloring book hashtag 90's synth.",         run: testsResult() },
    { description: "Hexagon enamel pin artisan drinking vinegar.",  run: testsResult() },
    { description: "Knausgaard semiotics fashion axe occupy health goth",       run: testsResult() },
  ];

方法


  uniqueId() {
    let strKeys = Object.keys(tests);
    tests.map(test => {
      for (let index = 0; index < strKeys.length; index++) {
        let uniqId = (strKeys[index] = parseInt(strKeys[index]));
        Object.assign(test, { id: uniqId });
      }
    });
  }

該方法使用單擊事件處理程序調用。 像這樣

this.uniqueId();

在設置任何 state 等之前。

而不是分配

它為每個測試記錄 testStatus [test.id] 的分配值到索引鍵

像這樣

0: "r"
1: "u"
2: "n"
3: "n"
4: "i"
5: "passed" (variable)
6: "g"

有人可以指出我的錯誤邏輯嗎?

代碼看起來有點混亂,但至少有一個明顯的點是

在下面的代碼中,為鍵id strKeys.length 次賦值是沒有意義的,因為在循環完成后只會分配最后一個

for (let index = 0; index < strKeys.length; index++) {
    let uniqId = (strKeys[index] = parseInt(strKeys[index]));
    Object.assign(test, { id: uniqId });
}

沒有test.id有嗎? 還是我錯過了什么

Object.assign旨在創建內部所有 object 屬性的新副本,它不會改變任何對象

如果您試圖改變tests以包含 id 那么我不確定您需要什么let strKeys = Object.keys(tests); for (let index = 0; index < strKeys.length; index++) {雖然

uniqueId() {
    let strKeys = Object.keys(tests);
    tests = tests.map(test => {
      let newTest = {};
      for (let index = 0; index < strKeys.length; index++) {
        let uniqId = (strKeys[index] = parseInt(strKeys[index]));
        newTest = Object.assign(test, { id: uniqId });
      }
      return newTest;
    });
  }

我不會試圖理解你為什么需要let strKeys = Object.keys(tests); for (let index = 0; index < strKeys.length; index++) {雖然。

我覺得這可能做同樣的事情

uniqueId() {
    tests = tests.map((test, index) => {
        let uniqId = parseInt(index);
        return Object.assign(test, { id: uniqId });
    });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM