2

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.

0

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.