简体   繁体   中英

Pulling single property from json when array of values are given

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.

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

 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.com/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 comparator 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.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM