This question already has an answer here:
ES6/ES2015 object destructuring and changing target variable
(1 answer)
Closed 3 years ago.
I want to quickly assign {x:1,y:2} to {row,col}, i.e. let row = objXY.x, let col = objXY.y.
I know the destructuring assignment can do this
let {x,y} = objXY
But is there any way to do like this?
let {row,col} = objXY
You can assign destructured properties to new variable names as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names
For example
const obj = {x: 1, y: 2};
const {x: row, y: col} = obj;
console.log(row); // 1
console.log(col); // 2
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:
Clone a object JSON but until its 5th key-value
(2 answers)
Closed 1 year ago.
Suppose I have an object with 10 key values,
const detail = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10}
I only want to get first 5 key values from object,
**{'a':1,'b':2,'c':3,'d':4,'e':5}**
Like we can slice in array and get first 5 elements, is it possible to do something for the same.
I tried looking for solution, but was unable to find anything related to it.
Any help would really be helpful. If anyone needs any moew details please let me know.
You can get an array of the object's entries (key-value pairs in the form of an array), then slice that array and turn it back into an object:
const detail = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10};
const sliced = Object.fromEntries(
Object.entries(detail).slice(0, 5)
);
console.log(sliced);
Use Object.fromEntires with Object.entries
Object.entries will return a nested array of key, value pairs, which you can slice and then pass that slice to Object.fromEntries which will return an object.
const detail = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 };
const res = Object.fromEntries(Object.entries(detail).slice(0, 5));
console.log(res);
This question already has answers here:
Copy array items into another array
(19 answers)
Closed 2 years ago.
I have this:
z: [[res.push(this.Map.heatmap[0])]],
so this is just one value. But how can I push all the values of the array
and this is the interface:
export interface map {
}
I have a array of 100 values in it:
10
but if I do this:
this.Map().subscribe((res) => {
zsmooth: 'best'
}
],
not all the values are loaded. So how to load all the values?
and this is how I have the object:
Map: map = {};
Thank you
Oke,
console.log(res)
gives back array with 100 values:
length: 100
but apperently this:
z: [[res]],
doesn't work. I dont see the value at all.
But if I do this:
hallo: console.log(res.push(this.Map.map[0])),
z: [[res.push(this.cMap.tmap[0])]],
it returns the number 2
concat function is maybe what you are looking for:
var array1 = [A, B, C];
var array2 = [D, E, F];
var newArray = array1.concat(array2);
The newArray will be [A,B,C,D,E,F]
In your case you would do something like:
z = z.concat(this.cameraAgretateHeadMap.heatmap)
a little bit more code from your side would have been helpful to understand it in a better what your problem is!
Hopefully this helps!
This question already has answers here:
How to deep merge instead of shallow merge?
(47 answers)
Closed 5 years ago.
I want to merge 2 objects where there's a similar prop on both and I want to have all props together:
let obj1 = {'foo': 'bar', 'far': {'tst': 1}}
let obj2 = {'far': {'other': 'token'}}
Object.assign({},obj1, obj2);
// Outputs: {"foo":"bar","far":{"other":"token"}}
// Desired output: {"foo":"bar","far":{'tst': 1, "other":"token"}}
Do I need to use the spread operator in some way?
Do you try it?
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
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.