How unpack the array with object? [duplicate] - javascript

This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
Closed 9 months ago.
How to get from this array
[{"one": "deleted"}, {"two": "added"}]
this result
{"one": "deleted", "two": "added"}

you can use Object.assign for that
const data = [{"one": "deleted"}, {"two": "added"}]
console.log(Object.assign({}, ...data))

Related

how to destruct this object with customer:id [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
How to use special characters (like hyphen) in destructuring assignment syntax? [duplicate]
(2 answers)
Closed 7 months ago.
I have an object with this data structure returned by api:
const obj = {customer:id: '123'}
How should I destruct this object? Thanks

Deconstructing arrays for the function arguments [duplicate]

This question already has answers here:
Passing an array as a function parameter in JavaScript
(12 answers)
Closed 6 years ago.
I have
function f(a,b,c) {}
and
const arr = [1,2,3]
What do I need to call
f(arr)
__________________________________________?
One this you can do is:
f.apply({}, arr);

How to access json object from array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
In my code one of the promise returning the result like this
[{"success":true,"fileName":"./tmp/test.txt"}]
and I want to convert it into Json. how to convert it?
output:-{"success":true,"fileName":"./tmp/test.txt"}
You receive an array containing one object.
Just access array's first element, like :
var array = [{"success":true,"fileName":"./tmp/test.txt"}];
var fileName = array[0].fileName;

Get values from a JavaScript array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
Say I have an array like:
var someArray = [{"id":1,"mid":"477","mname":"StackOverflow","image":"images/merchants/so.jpg"}, {"id":2,"mid":"478","mname":"Meta","image":"images/merchants/mt.jpg"}]
How do I get say the image string images/merchants/so.jpg for example?
someArray[0].image
or
someArray[0]['image']

What is the difference between `{ }` and `[ ]`? [duplicate]

This question already has answers here:
What is the difference between these arrays?
(5 answers)
Closed 9 years ago.
In JavaScript, what is the difference between
var stack = {};
and
var stack = [];
The first one is an empty object and the second is an empty array.

Categories

Resources