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)
Related
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:
Converting Json Object array Values to a single array
(5 answers)
Closed 2 years ago.
If I have an array such as:
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
Is it possible to make the array only containing the value pairs of the object such as:
let result = ["BSE","1010","STA","2020"];
Using Object.prototype.values, you can generate only values from an object.
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
const output = arr.flatMap((item) => Object.values(item));
console.log(output);
This question already has answers here:
Define an array with first element empty in JS [closed]
(2 answers)
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Javascript. Assign array values to multiple variables? [duplicate]
(2 answers)
Closed 3 years ago.
I'm learning how to use Nuxt to build generate a static blog, and I came across the piece of code bellow to create the page containing a list of posts:
<script>
export default {
async asyncData() {
const resolve = require.context("~/content/", true, /\.md$/)
const imports = resolve.keys().map((key) => {
const [, name] = key.match(/\/(.+)\.md$/);
return resolve(key);
});
return {
posts: imports
}
},
}
</script>
I understand what it does: getting a list of all the markdown files and map their keys to the file's name, but I don't understand what const [, name] means, actually what the coma inside the array means.
can somebody explain it to me, please?
Thanks.
Noah
It's called array destructuring.
In your case const [, name] = key.match(/\/(.+)\.md$/); is the same as const name = key.match(/\/(.+)\.md$/)[1]
It means take the second value from the array returned by key.match and assign it to the variable name
const [a, b] = [123, 456];
console.log('a:', a, 'b:', b); // a = 123, b = 456
const [, d] = [111, 222];
console.log('d:', d); // d = 222
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);
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);