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

javascript - Pulling single property from json when array of values are given - Stack Overflow

programmeradmin1浏览0评论

How can we retrieve name from the json, when array of ids are provided.

[
   {
      "id": 0,
      "name": "salesTransNo"
   },
   {
      "id": 1,
      "name": "terminalNo"
   },
   {
      "id": 2,
      "name": "salesTransDate"
   },
   {
      "id": 3,
      "name": "salesTransTime"
   },
   {
      "id": 4,
      "name": "exceptionAmount"
   },
   {
      "id": 5,
      "name": "laneNumber"
   }
]

I want to retrieve only names into an array from the JSON, when array of id values are given

eg: array of id's : [2,4,5]

Output should be:

["salesTransDate","exceptionAmount","LaneNumber"]

How can we achieve this with Lodash or with JavaScript ?

I used _.find and used _.map to pull only name from the result, but it's only working for single value, if I were to pass an array like [2,4,5] it's not working.

How can we retrieve name from the json, when array of ids are provided.

[
   {
      "id": 0,
      "name": "salesTransNo"
   },
   {
      "id": 1,
      "name": "terminalNo"
   },
   {
      "id": 2,
      "name": "salesTransDate"
   },
   {
      "id": 3,
      "name": "salesTransTime"
   },
   {
      "id": 4,
      "name": "exceptionAmount"
   },
   {
      "id": 5,
      "name": "laneNumber"
   }
]

I want to retrieve only names into an array from the JSON, when array of id values are given

eg: array of id's : [2,4,5]

Output should be:

["salesTransDate","exceptionAmount","LaneNumber"]

How can we achieve this with Lodash or with JavaScript ?

I used _.find and used _.map to pull only name from the result, but it's only working for single value, if I were to pass an array like [2,4,5] it's not working.

Share edited Apr 11, 2020 at 21:14 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Sep 22, 2017 at 19:30 VR1256VR1256 1,4606 gold badges38 silver badges65 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 4

You could filter the objects and then map the wanted property.

var data = [{ id: 0, name: "salesTransNo" }, { id: 1, name: "terminalNo" }, { id: 2, name: "salesTransDate" }, { id: 3, name: "salesTransTime" }, { id: 4, name: "exceptionAmount" }, { id: 5, name: "laneNumber" }],
    ids = [2, 4, 5],
    result = data
        .filter(({ id }) => ids.includes(id))
        .map(({ name }) => name);

console.log(result);

Vanilla JS:

var arr = [
   { "id": 0, "name": "salesTransNo"  },
   { "id": 1, "name": "terminalNo" },
   { "id": 2, "name": "salesTransDate" },
   { "id": 3, "name": "salesTransTime" },
   { "id": 4, "name": "exceptionAmount" },
   { "id": 5, "name": "laneNumber" }
];

var indexes = arr.map ( function ( d ) { return d.id; });

var id = 4; // Requested arr.id item
var select_name = arr[indexes.indexOf(id)].name;

If you wish to return multiple results, you can build a function like so:

function getNamesFromArr ( list_of_ids ) {
   var result = [];
   for ( var i = 0; i < list_of_ids.length; i++ ) {
      var indexes = arr.map ( function ( d ) { return d.id; });
      var select_name = arr[indexes.indexOf(list_of_ids[i])].name;
      result.push ( select_name );
   }
   return result;
}

getNamesFromArr ([ 2, 4, 5 ]); // Returns ["salesTransDate", "exceptionAmount", "laneNumber"]

Note: I had left out error handling for simplicity. Consider catching indexOf() values of -1.

var items = [{
    "id": 0,
    "name": "salesTransNo"
  },
  {
    "id": 1,
    "name": "terminalNo"
  },
  {
    "id": 2,
    "name": "salesTransDate"
  },
  {
    "id": 3,
    "name": "salesTransTime"
  },
  {
    "id": 4,
    "name": "exceptionAmount"
  },
  {
    "id": 5,
    "name": "laneNumber"
  }
]

var iname = items.filter(items => [2, 4, 5].includes(items.id));

for (var names of iname)
{console.log(names.name);}

You can do that with a lodash's chain using _.keyBy(), _.at(), and _.map():

var data = [{ id: 0, name: "salesTransNo" }, { id: 1, name: "terminalNo" }, { id: 2, name: "salesTransDate" }, { id: 3, name: "salesTransTime" }, { id: 4, name: "exceptionAmount" }, { id: 5, name: "laneNumber" }];
var ids = [2, 4, 5];

var result = _(data)
  .keyBy('id') // convert to a dictionary by id
  .at(ids) // get the items which id match the id array
  .map('name') // pluck the name
  .value();
  
console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

You can use lodash#intersectionWith, wherein the arguments order must be the collection first, the ids second and the parator at the end.

var result = _.intersectionWith(data, ids, (a, b) => a.id == b);

var data = [{
    id: 0,
    name: "salesTransNo"
  }, {
    id: 1,
    name: "terminalNo"
  }, {
    id: 2,
    name: "salesTransDate"
  }, {
    id: 3,
    name: "salesTransTime"
  }, {
    id: 4,
    name: "exceptionAmount"
  }, {
    id: 5,
    name: "laneNumber"
  }],
  ids = [2, 4, 5];
  
var result = _.intersectionWith(data, ids, (a, b) => a.id == b);

console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论