This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
var John = { Cats: 2, Dogs: 3, Turtles: 1 };
var Mary = { Dogs: 0, Parakeets: 3};
How do I append new dimensions after I've already created the objects?
...John now also has 1 Parakeet
...Mary now also has 5 Koi
Just give them a value, like:
John.Parakeet = 1;
You can then later access these properties just like any other properties.
It really isn't hard.
Related
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 months ago.
Im quite new to front-end development. I am using React.
My problem:
How to extract object value that are within array?
myArr = [[{a: 1, b: 2}], [{a: 1, b:2}], [{a: 1, b:2}]]
// I want to extract only the values of b and make them into array
// my asnwer should look like this:
// [2,2,2]
My Approach:
const [answerArr, setAnswerArr] = useState([]);
useEffect(() => {
const extractCode = () => {
const res = myArr.map((item)=>{
????
})
};
extractCode();
}, [codeArr]);
I've tried using map method...but I am struggling very much...
If you could help me i would learn so much!
Your code should look something like this
const res = myArr.map(item => item[0].b)
This question already has answers here:
Iterate through object properties
(31 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Let's say I have an object that has two key-values:
let object = {name: "abc", id: 12}
But let's say, I don't know one of the key-names, so:
let object = {<unknown_to_me>: "abc", id: 12}
How can I get the first key name if I know the other one? Their positions
THe first one is reachable via:
object.id
Can I get the other one by positions, the ! operator,...?
You could do so:
let object = {name: "abc", id: 12};
let knownKeyName = "id";
let objectKeys = Object.keys(object);
let unknownKeyName = objectKeys[objectKeys.length - 1 - objectKeys.indexOf(knownKeyName)];
console.log(unknownKeyName)
This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 1 year ago.
Hello i have an array of objects. Like this:
const obj = [{
count: 10,
value: count*2 // the previous count
}]
How can i reference 'count' on 'value' without having to find the index, or is a way to find the index of 'obj'?
You could take a getter with a reference to the same object.
const
objects = [{
count: 10,
get value () { return this.count * 2; }
}];
console.log(objects[0].value);
This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
What do these JavaScript bitwise operators do?
(3 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 3 years ago.
I'm following a web performance course where the following piece of code is shared:
// Interesting operator
const objects = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }];
let iterations = 10;
while (iterations--) {
let sum = 0;
const obj = objects[iterations & 3];
console.log(obj.a);
}
I've never seen anything like objects[iterations & 3] before. The loop will iterate through the array but it never goes out of bounds. I searched through MDN and other websites for more information but I couldn't find this operator.
Any pointers? Thanks!
Here is the example in code sandbox: https://codesandbox.io/s/dreamy-snowflake-gcmml?fontsize=14
This question already has answers here:
How to find the array index with a value?
(12 answers)
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 3 years ago.
Im trying to determine the location of a in a array. Im not really sure how to handle this case
const expect = require('chai').expect;
const answers = require('../src/arrays');
describe('arrays', function() {
let a;
beforeEach(function() {
a = [ 1, 2, 3, 4 ];
});
it('expect determine location of a in array', function(expect) {
expect(answers.indexOf(a, 3)).to.eql(2);
expect(answers.indexOf(a, 5)).to.eql(-1);
});
Try this!
var array = [ 1, 2, 3, 4 ];
function finding_index(element) {
return element === 2;
}
console.log(array.findIndex(finding_index));