简体   繁体   English

如何在对象中按键和值查找索引(但它完全没有数组的对象)

[英]How can I find index by key and value in an object (but its completely object without an array)

I am returning the data below from server side laravel.我正在从服务器端 Laravel 返回以下数据。

  response = {
    "1": {
      "id": 1,
      "title": "Port1",
      "code": "P",
      "accounting_code": "119"
    },
    "2": {
      "id": 2,
      "title": "Port2",
      "code": "P2",
      "accounting_code": "120"
    },
    "4": {
      "id": 4,
      "title": "Port3",
      "code": "P3",
      "accounting_code": "122"
    },
    "5": {
      "id": 5,
      "title": "Port4",
      "code": "P4",
      "accounting_code": "123"
    }
  }

The response here does not change even when I turn the collection into an array (toArray()).即使我将集合转换为数组 (toArray()),这里的响应也不会改变。 So I have to process this but when I use this code for example:所以我必须处理这个但是当我使用这个代码时:

response.findIndex( p => p.code == 'P1') // or .find() or .indexOf()

I get an findIndex is not a function error.我得到一个findIndex is not a function错误。 I know that it has to be an object array to use that function but there must be an easy way to easily process this laravel response on the fly.我知道它必须是一个对象数组才能使用该函数,但必须有一种简单的方法来轻松地即时处理此 Laravel 响应。 I mean using the laravels built in response, without converting it somehow.我的意思是使用响应中内置的 laravel,而不以某种方式转换它。

I think (for sure) I am missing a big concept here which I was not able to spot (realize) on the web just by googling.我认为(当然)我在这里遗漏了一个大概念,我无法通过谷歌搜索在网络上发现(实现)。

Turn the object into an array of entries (an entry is a key-value pair, as an array), and then you can use .find on that array.将对象转换为条目数组(条目是键值对,作为数组),然后您可以在该数组上使用.find If an entry matches the condition, you can extract the key (or value, or index) from it:如果条目与条件匹配,您可以从中提取键(或值或索引):

 const response = { "1": { "id": 1, "title": "Port1", "code": "P", "accounting_code": "119" }, "2": { "id": 2, "title": "Port2", "code": "P2", "accounting_code": "120" }, "4": { "id": 4, "title": "Port3", "code": "P3", "accounting_code": "122" }, "5": { "id": 5, "title": "Port4", "code": "P4", "accounting_code": "123" } }; const entry = Object.entries(response).find(([, obj]) => obj.code === 'P3'); if (entry) { console.log(entry[0]); }

If you don't care about the matching key or the value, just the index, then take the Object.values to get an array and call findIndex on it:如果您不关心匹配的键或值,只关心索引,则使用Object.values获取数组并对其调用findIndex

 const response = { "1": { "id": 1, "title": "Port1", "code": "P", "accounting_code": "119" }, "2": { "id": 2, "title": "Port2", "code": "P2", "accounting_code": "120" }, "4": { "id": 4, "title": "Port3", "code": "P3", "accounting_code": "122" }, "5": { "id": 5, "title": "Port4", "code": "P4", "accounting_code": "123" } }; const index = Object.values(response).findIndex(obj => obj.code === 'P3'); console.log(index);

But code generally shouldn't depend on keys of objects having a particular order.但是代码通常不应该依赖于具有特定顺序的对象的键。 While the order is deterministic, it can be somewhat confusing, and numeric array keys like 1 and 5 can only be "ordered" inside an object in ascending numeric order.虽然顺序是确定性的,但它可能会有些混乱,并且像15这样的数字数组键只能以数字升序在对象内部“排序”。 If you really need the index, you should consider changing the backend to send an array of objects instead of a single object.如果确实需要索引,则应考虑更改后端以发送对象数组而不是单个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM