This question already has answers here:
Map array by nested array object property, to an array of strings
(4 answers)
Closed 1 year ago.
I am learning javascript.
I have an array that looks like :
myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
I want to create a new array with ["Value1","Value2"].
How should I do that ?
Thanks a lot !
You can use the Javascript function map() for this. :
const myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
const r = myArray.map(e => e.item.fields.myfield)
console.log(r)
Related
This question already has answers here:
Flatten array with objects into 1 object
(12 answers)
Closed 1 year ago.
I have an array of objects in this shape;
const objectsArray = [{ projects: [] }, { components: [] }];
I want to merge it into a single object like this;
const mergedObject={ projects: [], components: [] }
Is there a better way of doing it other than this:
Object.keys(objectArray).map(key=>objectArray[key]).reduce((old,item)=>(
{...old,...item}
),{})
This might be a bit simpler:
const objectsArray = [{ projects: [1,2,3] }, { components: [4,5,6] }];
let mergedObjects
objectsArray.forEach(obj => mergedObjects = { ...mergedObjects, ...obj})
console.log(mergedObjects)
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}}
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Convert array of objects into array of properties [duplicate]
(4 answers)
Closed 2 years ago.
Is there a way to transform the following array of objects:
array = [
{ date: "08/18", id: 1 },
{ date: "08/14", id: 2 },
{ date: "08/15", id: 3 }
]
Into this? Only returning the dates:
array2 = ["08/18", "08/14", "08/15"]
I've been trying to push the date elements as it follows, but it doesn's seem to work:
array.map(e => {
array2.push(e.date)
})
Thanks in advance.
You can use the array map function:
array2 = array.map(object => object.date);
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Convert array of string to array of object using es6 or lodash
(4 answers)
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
I have an array like this:
const categories = ["Action", "Comedy", "Thriller", "Drama"]
And I would like to convert it to this format:
const displayedCategories = [{"type": "Action", "isDisplayed": true},{"type": "Comedy", "isDisplayed": true},{"type": "Thriller", "isDisplayed": true},{"type": "Drama", "isDisplayed": true}]
Any advice ? :)
You can do so by using map:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
const newCategories = categories.map(category => ({
type: category,
isDisplayed: true
}));
console.log(newCategories);
You can do so with the map method:
const displayedCategories = categories.map(function(category) {
return { type: category, isDisplayed: true };
});
Maybe like This:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
var out = [];
for(var key in categories){
out.push({"type": categories[key], "isDisplayed": true});
}
console.log(out);
use array.prototype.map to convert the array into an object
categories = catergories.map(val => {
var obj = {
type:val
//more properties go here
}
return obj
}
const categories = ["Action", "Comedy", "Thriller", "Drama"]
const displayedCategories = categories.map(category => { return {"type": category, "isDisplayed": true} })
console.log('displayedCategories :', displayedCategories);
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')
}