我想知道我们是否可以通过提供键值对来从json中获取项目。
例如我有一个像下面的json对象
[
{id: "864", text: "[145-028] ", name: "145028", type: null, description: ""},
{id: "593", text: "[111-111] ", name: "111111", type: null, description: ""},
{id: "616", text: "[330-00D] ", name: "33000D", type: null, description: ""},
{id: "595", text: "[124-964] ", name: "124964", type: null, description: ""},
{id: "597", text: "[476-978] ", name: "476978", type: null, description: ""},
{id: "131", text: "[142-222] ", name: "142222", type: null, description: ""},
{id: "132", text: "[149-603] ", name: "149603", type: null, description: ""},
{id: "603", text: "[778-498] ", name: "778498", type: null, description: ""}
]
如何获取ID为864
的单个商品,以便获取特定商品的名称,类型和说明。
任何帮助将不胜感激
答案 0 :(得分:2)
您可以使用filter
。它将返回另一个数组。使用index
获取对象并从中获取名称
let data = [{
id: "864",
text: "[145-028] ",
name: "145028",
type: null,
description: ""
},
{
id: "593",
text: "[111-111] ",
name: "111111",
type: null,
description: ""
},
{
id: "616",
text: "[330-00D] ",
name: "33000D",
type: null,
description: ""
},
{
id: "595",
text: "[124-964] ",
name: "124964",
type: null,
description: ""
},
{
id: "597",
text: "[476-978] ",
name: "476978",
type: null,
description: ""
},
{
id: "131",
text: "[142-222] ",
name: "142222",
type: null,
description: ""
},
{
id: "132",
text: "[149-603] ",
name: "149603",
type: null,
description: ""
},
{
id: "603",
text: "[778-498] ",
name: "778498",
type: null,
description: ""
}
];
function getval(id) {
let obj = data.filter(item => item.id === id);
return obj[0].name;
}
console.log(getval('864'))
或者,您也可以使用find
。与filter
不同,它将返回id
匹配的第一个对象。
let data = [{
id: "864",
text: "[145-028] ",
name: "145028",
type: null,
description: ""
},
{
id: "593",
text: "[111-111] ",
name: "111111",
type: null,
description: ""
},
{
id: "616",
text: "[330-00D] ",
name: "33000D",
type: null,
description: ""
},
{
id: "595",
text: "[124-964] ",
name: "124964",
type: null,
description: ""
},
{
id: "597",
text: "[476-978] ",
name: "476978",
type: null,
description: ""
},
{
id: "131",
text: "[142-222] ",
name: "142222",
type: null,
description: ""
},
{
id: "132",
text: "[149-603] ",
name: "149603",
type: null,
description: ""
},
{
id: "603",
text: "[778-498] ",
name: "778498",
type: null,
description: ""
}
];
function getvalUsingFind(id) {
let obj = data.find(item => item.id === id);
return obj;
}
console.log(getvalUsingFind('864').name)
答案 1 :(得分:0)
我建议:
const MyData = [{ 0: {id: "864", text: "[145-028] ", name: "145028", type: null, description: ""}
, 1: {id: "593", text: "[111-111] ", name: "111111", type: null, description: ""}
, 2: {id: "616", text: "[330-00D] ", name: "33000D", type: null, description: ""}
, 3: {id: "595", text: "[124-964] ", name: "124964", type: null, description: ""}
, 4: {id: "597", text: "[476-978] ", name: "476978", type: null, description: ""}
, 5: {id: "131", text: "[142-222] ", name: "142222", type: null, description: ""}
, 6: {id: "132", text: "[149-603] ", name: "149603", type: null, description: ""}
, 7: {id: "603", text: "[778-498] ", name: "778498", type: null, description: ""}
}];
function MyDataGetVal(x_ID) {
return Object.values(MyData[0]).find(E=>E.id===x_ID )|| null;
}
let E_864 = MyDataGetVal('864')
, E_132 = MyDataGetVal('132')
, E_XXX = MyDataGetVal('XXX')
;
console.log ('E_864 = ', JSON.stringify(E_864))
console.log ('E_132 = ', JSON.stringify(E_132))
console.log ('E_XXX = ', JSON.stringify(E_XXX))