JavsScript: Reduce array of dictionary into Array [duplicate] - javascript

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I get this response from REST API.
"items": [
{
"name": "one"
},
{
"name": "two"
}
]
I want to reduce it to array ["one", "two"]. Can someone please tell me how?

const res = {
items: [{
name: 'one'
},
{
name: 'two'
}
]
};
console.log(res.items.map(res => res.name));
try this

using map
var items = [
{
name: "one",
},
{
name: "two",
},
];
console.log(items.map((item) => item.name));

Related

How to sort a array data with another array order? [duplicate]

This question already has answers here:
Sort an array of objects based on another array of ids
(12 answers)
Closed 2 months ago.
I am searching any times, but I am not finding any solution exactly what I want. My question is How can I sort array with another array that defined the array order.
Suppose-
const array1 = [
{
name: "1"
},
{
name: "2"
},
{
name: "3"
}
]
const array2 = ["3", "1"]
I need to sort array1 by following
const array = [
{
name: "3"
},
{
name: "1"
},
{
name: "2"
}
]
Here, we can see array2 values are now top of array1. I want this function.
I already tried, but gives wrong result-
const sortMarkets = (array: ArrayTypes[], sortArray: string[]) => {
return [...array].sort(
(a, b) => sortArray.indexOf(a.name) - sortArray.indexOf(b.name)
)
}
console.log(sortMarkets(array, ag));
It gives me-
[{
"name": "2"
}, {
"name": "1"
}, {
"name": "3"
}]
Please help me.
You need to get another value for unknow items. In this case, a large value moves the item to the end.
const
array = [{ name: "1" }, { name: "2" }, { name: "3" }],
ag = ["3", "1"],
sortMarkets = (array, sortArray) => {
const getOrder = name => (sortArray.indexOf(name) + 1) || Number.MAX_VALUE;
return [...array].sort((a, b) => getOrder(a.name) - getOrder(b.name));
};
console.log(sortMarkets(array, ag));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Comparing the .indexOf with the value the other array isn't enough because there are two criteria to sort by - one, for elements in the other array to come on top of the others, and two, for those top elements to be sorted according to their position in the other array. Add another check first to see whether the items being compared should come in different sections (on the top or the bottom).
const array1 = [
{
name: "1"
},
{
name: "2"
},
{
name: "3"
}
];
const array2 = ["3", "1"];
array1.sort((a, b) =>
(array2.includes(b.name) - array2.includes(a.name))
|| (array2.indexOf(a.name) - array2.indexOf(b.name))
);
console.log(array1);
To make TypeScript happy, cast the booleans to numbers before subtracting.
(Number(array2.includes(b.name)) - Number(array2.includes(a.name)))

How can i find an array of objects out of an array [duplicate]

This question already has answers here:
How to traverse through array within an object and return each array element as the second key value pair, along with its' corresponding ID
(2 answers)
Closed 1 year ago.
i have an array
let data = [
{
A_ID: 'ABC',
C_Array: ["123","456","789"]
},
{
A_ID: 'DEF',
C_Array: ["444","555","666"]
}
]
and i want to create a new array like so that for each C_Array i get its coresponding A_ID,
like
let newData = [
{
A_ID: 'ABC',
C_ArrayValue: "123"
},
{
A_ID: 'ABC',
C_ArrayValue: "456"
},
]
How can i do that in JS
let data = [
{
A_ID: 'ABC',
C_Array: ["123", "456", "789"]
},
{
A_ID: 'DEF',
C_Array: ["444", "555", "666"]
}
]
const newArray = data.map((outerNode) => {
return outerNode.C_Array.map((innerNode) => {
return {
A_ID: outerNode.A_ID,
C_ArrayValue: innerNode
}
})
});
console.log(newArray);

foreach array with one key [duplicate]

This question already has answers here:
How to convert an array of objects to object with key value pairs
(6 answers)
Closed 1 year ago.
I've got an object with different values .
let arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},
{"Fashion": "1300"},{"Documentary": "2600"}]
How can I foreach them and then put them in an object and pick a name for them like this :
"photo_type": {
"Personal": "1000",
"sport": "2100",
"Industrial": "1200",
"Commercial": "2300",
"Fashion": "1300",
"Documentary": "2600"
}
I dont want them to be like 0 and 1 and 2.
You could create an object with Object.assign and by spreading the array.
let array = [{ Personal: "1000" }, { sport: "2100" }, { Industrial: "1200" }, { Commercial: "2300" }, { Fashion: "1300" }, { Documentary: "2600" }],
object = Object.assign({}, ...array);
console.log(object);
You can try this
const arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},{"Fashion": "1300"},{"Documentary": "2600"}]
let result = {}
arr.forEach(member => {result = {...result, ...member}}

Change array in to object in react js [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Convert Javascript array of objects into one object
(4 answers)
Closed 3 years ago.
This is my array format
let array = [
0: {
key:"name",
value: "John"
},
1: {
key:"age",
value:25
},
2: {
key:"job",
value:"software engineer"
},...
];
Change this array to object in given below format
{
name: "John",
age: 27,
job: "software engineer"
}
You can achieve it using forEach on the array.
Give this a try:
const array = [{
key: "name",
value: "John"
}, {
key: "age",
value: 25
}, {
key: "job",
value: "software engineer"
}];
const expected = {};
array.forEach(item => expected[item.key] = item.value);
console.log(expected);
You can use Array.prototype.reduce() to do this:
let array = [
{
key:"name",
value: "John"
},
{
key:"age",
value:25
},
{
key:"job",
value:"software engineer"
}
];
let result = array.reduce((a,b) => {
a[b.key] = b.value;
return a;
}, {});
console.log(result);
All you need to do is to loop over the array using forEach, for loop, while loop etcand push the values in a new object.
Also make sure that your array syntax is correct because the way you have mentioned it in the question is incorrect.
const data = [{ key:"name", value: "John" },{ key:"age", value:25 },{ key:"job", value:"software engineer" } ];
const res = {};
data.forEach(item => { res[item.key] = item.value});
console.log(res);

Javascript's .includes function not working correctly with array of objects [duplicate]

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 4 years ago.
I have an array of objects which I'm using the .includes() function. I'm searching this array with an object that is in the array (Objects are identical). However there doesn't appear to be a match. I have replicated the problem in this fiddle. The code is also below. So what is the correct way to check if an array contains am object?
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
if (list1.includes({
name: "object1"
})) {
document.write('contains')
} else {
document.write('doesnt')
}
You can't compare objects directly, but using this method , you can compare them with JSON.stringify.
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
var contains = list1.some(elem =>{
return JSON.stringify({name: "object1"}) === JSON.stringify(elem);
});
if (contains) {
document.write('contains')
} else {
document.write('doesnt')
}
You can try following
let list1 = [{name:"object1"},{name:"object2"},{name:"object3"},{name:"object4"}]
if (list1.some(({name}) => name === "object1")) {
document.write('contains')
} else {
document.write('doesnt')
}

Categories

Resources