Home > database >  searching for object in object variable
searching for object in object variable

Time:03-03

so i have a variable like this :

var userdata = {
  luk: {
    username: 'luktvpl',
    dcid: '53764947549426666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff1', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf7hhhcee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  },
  luk2: {
    username: 'luktvpl2',
    dcid: '53764947444666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff4', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf71-hee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  }

};

and i want to search this variable for user with for example uuid : "k9-bf71-hee-ffcb4fececb8" or with dcid : 53764947549426666 how can i do this in node.js and i want it to return user (luk/luk2)

CodePudding user response:

Use object destructuring for this.

let valuesToFind = ["53764947549426666", "k9-bf71-hee-ffcb4fececb8"];

const keys = Object.keys(userData);

keys.forEach((key, index) =>{
    const {dcid, UUID} = userData[key];
    if(dcid == valuesTofind[0] || UUID == valuesToFind[1]){
         console.log(`Found user ${userData[key}`);
    }
})

CodePudding user response:

In my solution i get the keys from all objects inner the one object. Then i iterate the keys and check if the UUID or dcid is in the object. then i check if the value of this keys is equal the search string / id.

 const userdata = {
  luk: {
    username: 'luktvpl',
    dcid: '53764947549426666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff1', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf7hhhcee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  },
  luk2: {
    username: 'luktvpl2',
    dcid: '53764947444666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff4', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf71-hee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  }

};


let results = [];
const toSearch = "53764947444666";

Object.keys(userdata).forEach(ob => {
  for(key in userdata[ob]) {
    if(key === 'dcid' || key === 'UUID') {
      if(userdata[ob][key].indexOf(toSearch)!=-1) {
        results.push(userdata[ob]);
        } 
    } 
  }
})


console.log("result", results)

CodePudding user response:

i figured it out. i should look like this:

//variable with data
var userdata = {
  luk: {
    username: 'luktvpl',
    dcid: '53764947549426666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff1', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf7hhhcee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  },
  luk2: {
    username: 'luktvpl2',
    dcid: '53764947444666',
    password: '',
    email: 'luk@...',
    xp: '0',
    lastwork: '',
    lastmessage: '',
    lastmailcode: '',
    inventory: [ 'staff4', 'kawa' ],
    statusy: {},
    osiagniecia: {},
    pety: {},
    UUID: 'k9-bf71-hee-ffcb4fececb8',
    IdBezpieczenstwa: ''
  }

};
//function for searching
function DbGetBy(what,data) {
    //generate array
    const keys = Object.keys(userdata);
    //look for user with "what" that have value "data"
    return keys.find((key,index)=>userdata[key][what]== data);  
};
//search for uuid : k9-bf7hhhcee-ffcb4fececb8
console.log(DbGetBy("UUID","k9-bf7hhhcee-ffcb4fececb8"));
  • Related