This question already has answers here:
Iterate over defined elements of a JS array
(7 answers)
Closed 3 years ago.
I'm new to coding and this situation poses quite a problem for me. I'm working with reactjs and inside a function I have props like this:
const{a,b,c,d,e,f,g,h} = props
So 'props' is an object, 'c' is an array, inside 'c' are 100 objects ranging from 'idx' 0 to 99, all these objects have an identical property call 'x', 'x' value is boolean 'true' or 'false'. I want to know the best way to loop through all these 100 objects so it will return 'true' or 'false'
Basically, c is like this:
let c = [{x:true},{x:true},{x:false}];
I only know so far as
console.log ('show value', props.c[idx])
to show key and value inside this object but cannot take the key I want by props.c[idx].x . I think I was wrong some where. Please help
Bonus: I want to know this so for every 'true', a button TRUE appear and vice versa, it's kinda like this
{!x (<button> False </button>)}
It's a bit hard to interpret your question. But wouldn't this is what you're looking for (I use array of 3 elements, but it should work for 100 of them):
let c = [{x:true},{x:true},{x:false}];
console.log(c.map(c=>c.x));
Let's assume that your data structure for props looks like this.
var props= {c : [{x:false},{x:true},{x:false},{x:true},{x:false},{x:true},{x:true}]}
Then your accessing method is indeed correct. Working snippet below!
var props= {c : [{x:false},{x:true},{x:false},{x:true},{x:false},{x:true},{x:true}]}
console.log("props.c[idx] value is : ",props.c[0])
console.log("props.c[idx].x value is : ",props.c[0].x)
console.log("=================================================");
//loop through all
for(idx in props.c){
console.log("Loop ",idx," : ",props.c[idx].x);
}
Related
This question already has answers here:
How do I test for an empty JavaScript object?
(48 answers)
Closed 1 year ago.
I would like to know a quick and clean way to check if objects with just __ob__ or __proto__ properties are empty. Now I have to "guess" a value inside my object and null check that instead of the object itself. Example:
const myObject = objectFromDb.innerValue ? objectFromDb : mockObject;
I would love to write this instead:
const myObject = objectFromDb || mockObject;
but it will return objectFromDb even if the values have not yet been retrieved. I'm fine with using a ternary statement instead if the || one, but I'm not happy with having to null-check an inner object. I'm sure there has to be a better way of doing this.
This worked:
Object.keys(objectFromDb).length > 0
(thanks to comment from Bergi)
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
So this is a bit of an unusual situation i am in but what i need to do is access the value of an object based on what index its stored in. The problem is that i need it in my Select component of material ui. So the overview is that i store alginment values of a video . the videos show up in a loop which means the select values are repeated and to know if its for the first video i append the index inside the object like this:
Now in order to set the option i have to access this value here :
Now the problem is in this loop i cant do something like :
halign.halign[index] because obviously that would mean its an array. So long story short how could i do something like :
value={`${halign.halign}${index}`}
So basically the end result for value to evaluate is : halign.halign0
and so on for each index.
NOTE the outer halign is the main useState object.
Remember that for JavaScript objects x['y'] and x.y are interchangeable.
So if you need to compute the key you're looking up, use:
halign['halign' + index]
Or template strings if you prefer.
Note this would be a lot easier if you organized your object with an internal array, so you could just do halign[index].
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 6 years ago.
This is related to the arrays in Javascript, which I am trying to use in a complex logic.
Consider the following code:
a['a1'] = 'AJA'
We know that, this is same as a.a1 = 'AJA' (provided proper definitions were given).
So, if we go ahead and interpret this:
console.log(a.a1[0])
console.log(a.a1[1])
console.log(a.a1[2])
console.log(a.a1)
It logs :
A
J
A
AJA
Now, all I need is to assign a new character at the 4th position.
When I try a[a1][3] = 'Y' or a.a1[3] = 'Y' and then try console.log(a.a1), It still displays AJA instead of AJAY.
I know that we can do this using string concatenation, i.e.
a['a1'] = a['a1'] + 'Y' and get this accomplished.
But why wasn't the first method working? By what other ways can do this?
Strings are immutable. It means that if you create a string, you can't modify it anymore. So your a1 doesn't know anything about 4th character.
You can see this example. I try to change the second char of the already created string, but it will not be changed anymore.
let a = {};
a['a1'] = 'AJA';
a.a1[1] = 'A';
console.log(a.a1);
For more you can see MDN Documentation
As I know a[a1][3] or a.a1[3] is a string variable, you can treat it as:
var s = 'ss';
When you evaluate s[0] you'll get a string value. So when you assign any string value to s, you'll not get 'ss' + anyvalue but anyvalue instead. :)
This question already has answers here:
JavaScript: Object Rename Key
(35 answers)
Closed 6 years ago.
Say I have some JSON data like this:
blah = [
{"Species":"setosa","Sepal.Length":5.1,"Sepal.Width":3.5},{"Species":"setosa","Sepal.Length":4.9,"Sepal.Width":3}
]
In my code, I won't necessarily know the key names of the JSON. Therefore, I can grab the keys like this (I do know that all elements of the array are identical in their keys):
mynames = Object.keys(blah[0]); // gives this... ["Species", "Sepal.Length", "Sepal.Width"]
I am able to change the name of the first key called Species here to newthing like this:
JSON.parse(JSON.stringify(blah).split('"Species":').join('"newthing":'));
But if I didn't know it was called 'Species', but knew it was the first element of 'mynames', I thought I could do the same thing like this:
JSON.parse(JSON.stringify(blah).split('mynames[0]:').join('"newthing":'));
or
JSON.parse(JSON.stringify(blah).split('"mynames[0]":').join('"newthing":'));
but both fail. Is there a way of doing this simply?
It seems what you want is
blah[0].newthing = blah[0][mynames[0]];
delete blah[0][mynames[0]];
but knew it was the first element of 'mynames',
Note that the order of keys is not guaranteed, so that might not work in every environment or even for multiple runs.
How do I remove a property from a JavaScript object? explains how the delete keyword can be used to remove properties. You can simply set the new property to the value of the old, then delete the old.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add a property to a Javascript Object using a variable as the name?
Dynamic property names for loop of object Javascript
In success of javascript function I receveing data and it look like this:
data.lvl1
data.lvl2
data.lvl3
...
let say I have only 3 elements in data and I would like to loop for each of them and rise alert for every level:
for(a = 1; a<= 3; a++)
{
alert(data.lvl + a);
//I would like to read lvl1, lvl2, lvl3
}
This approach is obviously wrong.
Please explain how to reach lvl1, lvl2 in loop when lvl number is based on increasing a.
If you want to access a property name using a string, then use square bracket notation.
foo.bar === foo['bar']
Such:
alert(data['lvl' + a]);
But it would be better to restructure your data so that you had something like:
data = { lvl: [1,2,3] }
instead of
data = { lvl1: 1, lvl2: 2, lvl3: 3 }