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
    *"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. – T.J. Crowder Jul 12 '21 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, ...}` – Sajeeb Ahamed Jul 12 '21 at 13:33
  • Related: http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable, http://stackoverflow.com/questions/14442896/dynamic-variable-name-in-loop, https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name – T.J. Crowder Jul 12 '21 at 13:36

2 Answers2

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    thanks a lot, my code got simpler now by changing the array into an object. – ammar mohamed Jul 12 '21 at 14:43
  • 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. – ammar mohamed Jul 18 '21 at 07:55
  • @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? – T.J. Crowder Jul 18 '21 at 07:58
  • https://stackoverflow.com/questions/68428457/how-to-match-a-custom-data-value-with-an-object-containing-variables-in-js this is where i posted the question. may you help me? – ammar mohamed Jul 18 '21 at 11:08
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 }
Bharat
  • 1,192
  • 7
  • 14