最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get item from json object with key:value - Stack Overflow

programmeradmin2浏览0评论

I would like know if we can get an item from json by providing a key value pair.

For eg. I have a json object like below

[
{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: ""}
]

How can I get a single item by id 864, so that I can get the name, type and description for the particular item.

Any help would be greatly appreciated

I would like know if we can get an item from json by providing a key value pair.

For eg. I have a json object like below

[
{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: ""}
]

How can I get a single item by id 864, so that I can get the name, type and description for the particular item.

Any help would be greatly appreciated

Share Improve this question edited Jun 8, 2019 at 14:20 Munawir asked Jun 8, 2019 at 13:07 MunawirMunawir 3,3569 gold badges35 silver badges51 bronze badges 3
  • 1 Possible duplicate of How can I access and process nested objects, arrays or JSON? – str Commented Jun 8, 2019 at 13:12
  • can you please clarify if the object has keys like 0,1,2 or you just copied from console? – brk Commented Jun 8, 2019 at 13:29
  • What's a JSON object? – Dexygen Commented Jun 8, 2019 at 14:10
Add a ment  | 

2 Answers 2

Reset to default 7

You can use filter. It will return another array. Use index to get the object and retrieve name from it

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'))

Alternatively you can also use find. Unlike filter it will return the first object where the id matches.

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) {

  return data.find(item => item.id === id).name;
}

console.log(getvalUsingFind('864'))

I propose:

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))

发布评论

评论列表(0)

  1. 暂无评论