打字稿:打印包含数组的对象的键/值(无forEach)

时间:2020-01-15 18:08:27

标签: arrays typescript object foreach key

我正在尝试从包含数组的对象中提取值。 对象的外观如下:

export const myBookList: Row[] =
[
    {
        bookInfo: [
            {
                title: "The jungle book",   
                isbn: "42398486965239671234",
                Author: "Rudyard Kipling"
            },
            {
                title: "20,000 Leagues Under the Sea",
                isbn: "42234486234239675678",
                Author: "Jules Verne"
            },
            {
                title: "The Call of the Wild",
                isbn: "4239848696523967670",
                Author: "Jack London"
            }
        ],
        value: [],
        id: 0
    },
    {
        bookInfo: [
            {
                title: "The jungle book 2",   
                isbn: "327865923874659276",
                Author: "Rudyard Kipling"
            },
            {
                title: "20,000 Leagues Under the Sea 2",
                isbn: "269587649587264675",
                Author: "Jules Verne"
            },
            {
                title: "The Call of the Wild",
                isbn: "2378687827584758765",
                Author: "Rudyard Kipling"
            }
        ],
        value: [],
        id: 1
    }
];

我想遍历对象的所有键和值(尤其是数组bookInfo中的键/值),当找到与传递的字符串参数“ myKey”匹配的键时,我想返回核心价值。 下面的代码可以正常工作,但是由于myBookList上的条目不止一个...当我发现我匹配时,由于每次迭代我都不能退出该过程(打字稿中的forEach不接受break或return)并且我总是得到对象的最后一个条目。 该如何解决?

getValue(data: Row, myKey: string): string {
let retValue: string = "";
data.forEach(function(item) {
    item.bookInfo.forEach(function(key, index){
        console.log(key, index);
        if (key===myKey) {
            console.log(key, index);
            ret=key.value;
        }

     });
});
return retValue;

}

1 个答案:

答案 0 :(得分:0)

您可以使用while循环。当您满足条件时,我们可以使用return语句来获取所需的值:

const getValue = (data, myKey) => {
  let i = 0;

  while (data.length > i) {
      let bookInfoIndex = data[i].bookInfo
                                 .findIndex(f => Object.keys(f).includes(myKey));
      if (bookInfoIndex != -1)
        return data[i].bookInfo[bookInfoIndex][myKey];

      i++;
  }
}

一个例子:

let arr = [{bookInfo: [{title: "The jungle book",isbn: "42398486965239671234",Author: "Rudyard Kipling"}, {title: "20,000 Leagues Under the Sea",isbn: "42234486234239675678",Author: "Jules Verne"      },{title: "The Call of the Wild",isbn: "4239848696523967670",Author: "Jack London"}],  value: [],    id: 0  },
  { bookInfo: [{title: "The jungle book 2",isbn: "327865923874659276",Author: "Rudyard Kipling" },   {title: "20,000 Leagues Under the Sea 2",isbn: "269587649587264675",Author: "Jules Verne"}, {title: "The Call of the Wild",isbn: "2378687827584758765",Author: "Rudyard Kipling"}], value: [], id: 1 }];

const getValue = (data, myKey) => {
  let i = 0;

  while (data.length > i) {
      let bookInfoIndex = data[i].bookInfo.findIndex(f => Object.keys(f).includes(myKey));
      if (bookInfoIndex != -1)
        return data[i].bookInfo[bookInfoIndex][myKey];

      i++;
  }
}

console.log(getValue(arr, 'title'));