【问题标题】:javascript filter json where data equal valuejavascript过滤json,其中数据等于值
【发布时间】:2023-01-12 23:39:43
【问题描述】:

我有以下 json 对象。我正在尝试过滤数据并获取名称等于 bq89 的 socketId 值```名称:“bq89”

const rooms = {
    "room1": {
        "socketId1":{
            id: "123",
            name: "the person name 1"
        },
        "socketId2":{
            id: "bq89",
            name: "the person name 2"
        }
    },
    "room2": {
        "socketId11":{
            id: "jkl",
            name: "room 2 name 1"
        },
        "socketId22":{
            id: "lpo",
            name: "room 2 name 2"
        }
    }
}

const socketId = rooms['room1'].filter(e=> {return e.name === 'bq89'})

console.log(socketId)
// desired output would be: socketId2

【问题讨论】:

  • filter 是为了数组,但这里没有数组。您是否打算使用数组而不是编号的对象属性? (你可能应该这样做。)
  • 使用来自 api 的数据。
  • 在这种情况下,如果 API 返回格式不正确的数据,那么您需要解决这个问题。例如,您可以开始 here 以迭代对象属性并构造一个仅包含您想要的属性的新对象。总体而言,您拥有的是一个损坏的数据结构,因此您需要编写自定义代码来对其执行标准操作。 (或者编写代码将结构转换为更有用的东西,然后对其执行标准操作。)

标签: javascript


【解决方案1】:

缺少访问整个 rooms 对象的答案。

const rooms = {
    "room1": {
        "socketId1": {
            id: "123",
            name: "the person name 1"
        },
        "socketId2": {
            id: "bq89",
            name: "bq89"
        }
    },
    "room2": {
        "socketId11": {
            id: "jkl",
            name: "room 2 name 1"
        },
        "socketId22": {
            id: "lpo",
            name: "room 2 name 2"
        }
    }
}

//flatten the rooms object
//returning a single object where the props are the socketids with their corresponding values
const sockets = Object.entries(rooms).reduce( (merge,[roomname, sockets])=>{ return {...sockets, ...merge} }, {} );

function findSocketsIdByName(name){
  //returns an array where each item is a socket being an array of 2 elements [socketid,{...props}]
  const socketsFound = Object.entries(sockets).filter( ([socketid, props]) => props.name == name);
  //returns an array with only the socketid of the object matching the wanted name
  const socketsIdFound = socketsFound.map( socket => socket[0]);

  return socketsIdFound;
}

let idFound;

idFound = findSocketsIdByName('bq89');
console.log(idFound);

idFound = findSocketsIdByName('room 2 name 2');
console.log(idFound);

【讨论】:

    【解决方案2】:

    使用 Object.entries 将 room1 转换为条目数组允许您使用数组方法,例如 Array.findArray.filter。你说你想“过滤名称等于 bq89 的 socketId 值”(单数)因此我假设“过滤器”实际上是指“查找”。

    const rooms = {
        "room1": {
            "socketId1": {
                id: "123",
                name: "the person name 1"
            },
            "socketId2": {
                id: "bq89",
                name: "bq89"
            }
        },
        "room2": {
            "socketId11": {
                id: "jkl",
                name: "room 2 name 1"
            },
            "socketId22": {
                id: "lpo",
                name: "room 2 name 2"
            }
        }
    }
    
    const socketId = Object.entries(rooms["room1"]) // this can be `rooms.room1`
      .find(([key, value]) => value.name === "bq89")[0];
    
    // desired output would be: socketId2
    console.log(socketId);

    更一般地说,您从 API 接收的数据已损坏且格式非常规。如果您有权访问 API,我建议您更改数据的结构方式。

    【讨论】:

    • 如果我想要名称为room 2 name 1 的套接字会怎样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多