-1

I have a following JsonArray:

arr = [
      {
        "name": "test",
        "alias": "alias1",
        "type": 1
      },
      {
        "name": "test",
        "type": 0
      },
      {
        "name": "abc",
        "alias": "alias2",
        "type": 1
      }
    ]

And I want to find using a variable value (which may contain alias / key). So basically the first preference of find should be alias, and if alias with the same value is not found then it should search in "name" and where "alias" is not present.

Normally it would have gone like:

_.find(arr, {
 alias: value 
})

But I want the code to return me the obj where name = value , if alias=value is not found

1) Ex: value = "alias1" Expected==>

{
    "name": "test",
    "alias": "alias1",
    "type": 1
  }

2) Ex: value = "test" Expected==>

{
        "name": "test",
        "type": 0
      }
  • _"I have a following JsonArray"_ - No, you have not. That's an array of objects ([What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation)). – Andreas Feb 25 '20 at 16:52
  • I can't quite tell what's being asked here. For this example array `arr`, what is the correct answer? Is it `1`? – Jason Orendorff Feb 25 '20 at 16:56
  • Basically I want to use the _.find in such a way that first it finds on basis of "alias". And if not found, finds for "name" –  Feb 25 '20 at 16:58

3 Answers3

3

You'll need to use find (_.find() or Array.find()) to look for the alias match, and if none found use find again to look for a name match:

const findAliasOrName = (value, arr) =>
  _.find(arr, { alias: value }) || _.find(arr, { name: value });

const arr = [{"name":"test","type":0},{"name":"test","alias":"alias1","type":1},{"name":"abc","alias":"alias2","type":1}]

console.log(findAliasOrName('alias1', arr));
console.log(findAliasOrName('test', arr));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
2

you can do this as follow:

if the alias key is available in obj then match the value with alias else match the value with name key using ternary operator

var arr = [
      {
        "name": "test",
        "alias": "alias1",
        "type": 1
      },
      {
        "name": "test",
        "type": 0
      },
      {
        "name": "abc",
        "alias": "alias2",
        "type": 1
      }
    ];

const findValue = (arr, value) => {
    return _.find(arr, (elem) => {
        return elem.alias ? elem.alias === value : elem.name === value;
    });
}

console.log(findValue(arr, 'alias1'));
console.log(findValue(arr, 'test'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Tyagi
  • 699
  • 6
  • 6
0

You need to take a custom callback which takes the two possible properties for checking the value.

function find(array, value) {
    return _.find(array, o => o.alias === value || o.name === value)
}

var array = [{ name: "abc", alias: "abc_alias", type: 1 }, { name: "tofind", type: 2 }, { name: "def", alias: "al1", type: 3 }];

console.log(find(array, 'abc_alias'));
console.log(find(array, 'tofind'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js">
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I want to give preference to "alias" over "name". So basically for code[ { "name": "value", "type": 0 }, { "name": "abc", "alias": "value", "type": 1 } ]code. I want it to return code { "name": "abc", "alias": "value", "type": 1 } –  Feb 25 '20 at 17:17
  • the above should work like this. if you need a different preferece, change the order of the conditions. – Nina Scholz Feb 25 '20 at 17:42
  • I think the point is that the OP wants the behavior found in Ori Drori's answer: find the first one that matches `alias`, and if none do, find the first one that matches `name`. This answer returns the first one that matches *either* `alias` or `name`. – Scott Sauyet Feb 25 '20 at 19:16