Home > Back-end >  How to check if the input matches the value in an array of objects
How to check if the input matches the value in an array of objects

Time:11-09

this.wiredUserData =
  [
    {"result":
    {"fields":
    {"Id":{"value":"005xx000001X85BAAS"},
     "Username":{"value":"[email protected]"}}}}, 
    {"result":
    {"fields":
    {"Id":{"value":"005xx000001X7sHAAS"},
     "Username":{"value":"[email protected]"}}}}
  ]

const uid = "005xx000001X85B"

usernameData(uid) {
    if (this.wiredUserData) {
        this.wiredUserData.forEach(function (item) {
            let wiredUserId = item.result.fields.Id.value.slice(0, 15);
            console.log(wiredUserId, "wired");
            console.log(uid, "uid");
            let wiredUsername = item.result.fields.Username.value;
            if (uid === wiredUserId) {
                return wiredUsername;
            }
        });
    }
    return '';
}

I am attempting to return the username value (e.g. [email protected]) when the function is called if the uid and Id match.

Hi, I am looping over wiredUserData and getting an error Expected to return a value at the end of function. What am I missing here? Should I use another kind of for loop? Thanks in advance.

CodePudding user response:

You can't return out of forEach statements (use a for loop instead):

const wiredUserData = [{
    "result": {
      "fields": {
        "Id": {
          "value": "005xx000001X85BAAS"
        },
        "Username": {
          "value": "[email protected]"
        }
      }
    }
  },
  {
    "result": {
      "fields": {
        "Id": {
          "value": "005xx000001X7sHAAS"
        },
        "Username": {
          "value": "[email protected]"
        }
      }
    }
  }
]

const uid = "005xx000001X85B"

function usernameData(uid) {
  if (wiredUserData) {
    for (const item of wiredUserData) {
      if (item.result.fields.Id.value.slice(0, 15) === uid) {
        return item.result.fields.Username.value
      }
    }
  }
  return '';
}

console.log(usernameData(uid))

CodePudding user response:

You shouldn't use .forEach functional loop when you want to return something.

Either you can do this

function usernameData(uid) {
  if (!this.wiredUserData) return;

  let tempResult = '';

    for (const data of this.wiredUserData) {
      let wiredUserId = data.result.fields.Id.value.slice(0, 15);
      let wiredUsername = data.result.fields.Username.value;
      if (uid === wiredUserId) {
        tempResult = wiredUsername;
      }
    }
  
  return tempResult;
}

or this

function usernameData(uid) {
  if (!this.wiredUserData) return;

    for (const data of this.wiredUserData) {
      let wiredUserId = data.result.fields.Id.value.slice(0, 15);
      let wiredUsername = data.result.fields.Username.value;
      if (uid === wiredUserId) {
        return wiredUsername;
      }
    }
  
  return '';
}

or this

function usernameData(uid) {
  if (!this.wiredUserData) return;

    for (const data of this.wiredUserData) {
      if (data.result.fields.Id.value.slice(0, 15) === uid) {
        // use null aware operator for more checking 
        // like data.result.fields.Username?.value ?? '';
        // can use in other example of above as well.
        return data.result.fields.Username.value;
      }
    }
  
  return '';
}

Thanks, if anything else or it is still not working comment on it again.

CodePudding user response:

Especially if the idea is to do this operation repeatedly and/or on a larger array, it would serve you to index the userData by the user ids...

In other words, if there was an index that looked like this...

this.userDataIndex = {
  '005xx000001X85B' : { /* the object with the 85B id */ },
  '005xx000001X7sH' : { /* the object with the 7sH id */ },
  // ...
}

...the username method would be as simple as this...

usernameData(uid) {
  return this.userDataIndex[uid] || '';
}

So all that's needed is the userDataIndex prop. This can be computed when the wiredUserData is set. One idea would be to do that in a setter...

set wiredUserData(wiredUserData) {
  this._wiredUserData = wiredUserData;
  this.userDataIndex = wiredUserData.reduce((acc, el) => {
    const id = el.result.fields.Id.value.slice(0, 15);
    acc[id[] = el;
    return acc;
  }, {});
}

get wiredUserData() {
  this._wiredUserData
}

CodePudding user response:

you should be able to use Array.find() if you know the structure of the JSON in your array.

const result = this.wiredUserData.find(element => element.result.fields.Id.value == uid);

This will return any object whose result.fields.Id.value matches the uid provided, or undefined if none match. You can also use Array.findIndex() with a similar callback if you need the index of the result.

ref: Array.find() Array.findIndex()

  • Related