【问题标题】:How to get the last item in an array of objects where the value to a key has a specific character?如何获取对象数组中键的值具有特定字符的最后一项?
【发布时间】:2021-07-15 21:58:38
【问题描述】:

使用以下数组:

arrOfOranges =
[{ "kerp": "thisThing", "time": "@ rocks 3"},
{ "kerp": "otherThing", "green": "blah"},
{ "kerp": "thisThing", "time": "(countriesToTheStart ^"},
{ "kerp": "anotherThing", "yellow": "row row"},
{ "kerp": "anotherThing", "yellow": "rigorous"},
{ "kerp": "thisThing", "time": ",,,,"}]

每个项目都有以下永久结构:

[{kerp: ''}] 可以是 3 个值之一:thisThing、otherThing 和 anotherThing。每个对象的另一个键取决于 'kerp' 的值。

如果 kerp: 'thisThing' 那么另一个键是 'time'。 => { "kerp": "thisThing", "time": ""},

如果 kerp: 'otherThing' 那么另一个键是 'green'。 => { "kerp": "otherThing", "green": ""},

如果 kerp: 'anotherThing' 那么另一个键是 'yellow'。 => { "kerp": "anotherThing", "yellow": ""},

我想用“kerp”获取数组中的最后一项:“thisThing”。但是,“时间”键的关联值必须包含一个字母。

例如在上面的数组中,数组中的最后一项有 'kerp': 'thisThing',但是 'time' 不包含字母。

所以我需要从 arrOfOranges 获取这个项目,它是数组中的最后一个项目,带有 kerp: thisThing 和 time,值中有字母:

{"kerp": "thisThing", "time": "(countriesToTheStart ^"}

这是我目前所拥有的:

if (arrOfOranges.filter(a=>a.kerp === "thisThing")) {
    // this is just a eay to check if the array contains letters
    if(arrOfOranges.time.match(/[\w]+/g))  {
        return arrOfOranges.slice(-1)[0];
    }
}

但是它没有按预期工作。我怎么能做到这一点?

【问题讨论】:

  • Object.values(arrOfOranges).reverse().filter(f => f.time && /[a-z]/i.test(f.time)).pop()

标签: javascript arrays json key-value


【解决方案1】:

您需要将检查保留在过滤器内。

const arrOfOranges = [
  { "kerp": "thisThing", "time": "@ rocks 3"},
  { "kerp": "otherThing", "green": "blah"},
  { "kerp": "thisThing", "time": "(countriesToTheStart ^"},
  { "kerp": "anotherThing", "yellow": "row row"},
  { "kerp": "anotherThing", "yellow": "rigorous"},
  { "kerp": "thisThing", "time": ",,,,"}
 ]

const result = arrOfOranges.filter(orange => orange.kerp === "thisThing" && orange.time.match(/[\w]+/g));

console.log(result);

【讨论】:

    【解决方案2】:

    您可以使用.reverse() 反转您的arrOfOranges,然后在该反转数组上使用.find()。 find 方法将返回回调为true 返回的第一个对象。下面,当time 键包含单词字符\w 并且kerp 键等于"thisThing" 时,我已使其返回true。请注意,我在调用.reverse() 之前使用了.slice(),因为反向会修改arrOfOranges,这可以通过先复制数组来防止:

    const arrOfOranges = [{ "kerp": "thisThing", "time": "@ rocks 3"}, { "kerp": "otherThing", "green": "blah"}, { "kerp": "thisThing", "time": "(countriesToTheStart ^"}, { "kerp": "anotherThing", "yellow": "row row"}, { "kerp": "anotherThing", "yellow": "rigorous"}, { "kerp": "thisThing", "time": ",,,,"}];
    
    const search = {kerp: "thisThing", key: "time"};
    const res = arrOfOranges.slice().reverse().find(
      obj => obj.kerp === search.kerp && /\w/.test(obj[search.key])
    );
    
    console.log(res);

    如果您需要更高效的方法,您可以使用标准的 for 循环来向后遍历您的数组:

    const arrOfOranges = [{ "kerp": "thisThing", "time": "@ rocks 3"}, { "kerp": "otherThing", "green": "blah"}, { "kerp": "thisThing", "time": "(countriesToTheStart ^"}, { "kerp": "anotherThing", "yellow": "row row"}, { "kerp": "anotherThing", "yellow": "rigorous"}, { "kerp": "thisThing", "time": ",,,,"}];
    
    const search = {kerp: "thisThing", key: "time"};
    function findObj(data, search) {
      for(let i = data.length-1; i >= 0; i--) {
        const curr = data[i];
        if(curr.kerp === search.kerp && /\w/.test(curr[search.key]))
          return curr;
      }
    }
    
    console.log(findObj(arrOfOranges, search));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-10
      • 2022-08-03
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2021-12-30
      相关资源
      最近更新 更多