0

filteredArray is an array of variables So, I want to get a variable from it by searching in to it using a string and I want directly to use this variable in another code, can you help me?

     
      const filteredArray = [livingRrooms, kitchens, ceilingsDesigns, bedroomsArray ];
    // z is the string I get from a code that i have
         const z = "livingRrooms kitchens ceilingsDesigns bedroomsArray";
           function checkvar(cas) {
               return z.includes(cas);
                                 }
      // as you can see next line is working just find     
           console.log(checkvar("kitchens"));
     // this dosn't work because find use a srtict mode I need a way around it or anonther way
           console.log(filteredArray.find(checkvar));
3
  • 3
    "filteredArray is an array of variables" No, it's an array of values. Those values came from some variables, but there is no link from the array back to the variables where the values came from. Commented Jul 12, 2021 at 13:31
  • If you need not to use repeating values to the filteredArray then you can change the data structure from Array to Object. Do it like const data = {livingRooms, kitchens, ceilingsDesigns, bedroomsArray, ...} Commented Jul 12, 2021 at 13:33
  • Related: stackoverflow.com/questions/4244896/…, stackoverflow.com/questions/14442896/…, stackoverflow.com/questions/695050/… Commented Jul 12, 2021 at 13:36

2 Answers 2

2

filteredArray is an array of variables

No, it's an array of values. Those values came from some variables, but there is no link from the array back to the variables where the values came from. If (for instance) both livingRooms and kitchens have the value 5, you have no way of knowing (at runtime) where either of the 5s in the array came from.

If you want to look for variables by name, use the variables to create object properties:

const items = {livingRooms, kitches, ceilingsDesigns/*...*/};

Then if you have x with the value "livingRooms", you can use items[x] to get the value for the livingRooms property from items.

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

4 Comments

thanks a lot, my code got simpler now by changing the array into an object.
but how can I make a link between the value of the variable and the object that has the the variable, because when I fetch the variable from the object I don't get the initial value of it instead I get an object and when I console the length of the fetched variable it get me the length of the string of the variable instead of the Array length that the variable has as a value.
@ammarmohamed - I'm afraid I don't quite understand what you mean by that. Perhaps post a new question showing the problem and explaining what result you're trying to get?
stackoverflow.com/questions/68428457/… this is where i posted the question. may you help me?
0

I agree with what Sajeeb proposed

const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
];

function isCherries(fruit) {
  return fruit.name === 'cherries';
}

console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }

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.