// For demo purposes, I have added the JSON object directly in the code.
// But in your code, you can just do something like this:
// const entries = require("./filepath/to/json/file")
const entries = [{
"name": "adam",
"age": "16",
"id": 9
},
{
"name": "jack",
"age": "18",
"id": 10
}
]
const objId = 9
const filteredObj = entries.filter((obj) => obj.id === objId)[0]
const name = filteredObj.name
const age = filteredObj.age
const id = filteredObj.id
console.log("Name: ", name)
console.log("Age: ", age)
console.log("Id: ", id)
1条答案
按热度按时间qyswt5oh1#
可以先使用require导入JSON文件,然后过滤这些文件以找到具有所需特定ID的条目,然后响应交互。