This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
I am trying to convert the "count" key from each object within the test array into a new array so I end up with something like
newCount =[0,0,0,0]
const test = [
{
id: 0,
count: 0,
image: "",
text: 'Some text about finn'
},
{
id: 1,
count: 0,
image: "",
text: 'Some text about daphne'
},
{
id: 2,
count: 0,
image: "",
text: 'Some text finn'
},
{
id: 3,
count: 0,
image: "",
text: 'Some text daphne'
}
]
You can use Array.prototype.map() on your test array to extract the count property value of each object:
const newCount = test.map(t => t.count);
Hopefully that helps!
You can use a map on the array:
const newarray = test.map(item => item.count);
Array map documentation
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I want to construct a JSON object from the result obtained by iterating through the array below.
Here is the array list : Input
var input = [
{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
},
{
bio: "Test2",
id: 3,
image: "http://localhost:8000/media/default.png",
user: 2
}
]
I want something like the below:
Expected output:
{
"Test": {
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
},
"TestTwo": {
bio: "TestTwo",
id: 3,
image: "http://localhost:8000/mediafile/default.jpg",
}
}
I am able to get array of objects but not the exact format that I want. Need to call ajax with the same output.
var input = [
{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
},
{
bio: "Test2",
id: 3,
image: "http://localhost:8000/media/default.png",
user: 2
}
];
const final = input.reduce((res, obj) => {
res[obj.bio] = obj;
return res;
}, {});
console.log(final);
Previous answer you asked to do the opposite, and my answer was:
Use Object.values, and add user property with value 2:
const input = {
Test: {
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
},
TestTwo: {
bio: "TestTwo",
id: 3,
image: "http://localhost:8000/mediafile/default.jpg",
},
};
const objs = Object.values(input);
for (let obj of objs) {
obj.user = 2
}
console.log(JSON.stringify(objs, null, 4))
/*
[
{
"bio": "Test",
"id": 2,
"image": "http://localhost:8000/media/default.jpg",
"user": 2
},
{
"bio": "TestTwo",
"id": 3,
"image": "http://localhost:8000/mediafile/default.jpg",
"user": 2
}
]
*/
Other ways to add property user:
You could use Use Array.prototype.map()
objs = objs.map(obj => ({ ...obj, user: 2 }))
or use with more readable with object assign
objs = objs.map(obj => {
const userPropsObj = {
user: 2
};
return Object.assign(obj, userPropsObj);
});
The answer to this question which tend to return back to the initial input(object) instead of an array:
you could iterate over the array and delete user property we add and assign each object with bio as the key.
const input = [
{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
},
{
bio: "Test2",
id: 3,
image: "http://localhost:8000/media/default.png",
user: 2
}
];
const output = {};
for (let obj of input) {
delete obj.user;
output[obj.bio] = obj;
}
console.log(output)
PS: Please, Don't delete the question as you did before because you delete the answers we did too, And maybe answers help someone else!
const obj = input.reduce((result, item) => { result[item.bio] = item;
return result;
}, {});
reduce is a higher-order function it takes a method as an argument that method contains the result and items in my example; it iterates over each item in the array and assigns the whole object value to the key we created which is item.bio accumulating all these keys will results in one object with the keys and values as needed.
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have an array of teachers, where each one have a property user with image property, that can contain a string or null value.
How can i order the array, considering the user.image property?
Here is an example of structure.
const teachers = [
{
id: 1,
user: {
id: 10,
image: "image.jgp"
}
},
{
id: 3,
user: {
id: 30,
image: null
}
},
{
id: 2,
user: {
id: 20,
image: "image.jgp"
}
},
{
id: 4,
user: {
id: 40,
image: null
}
},
]
You can use Array.sort and check whether the image property is null:
const teachers=[{id:1,user:{id:10,image:"image.jgp"}},{id:3,user:{id:30,image:null}},{id:2,user:{id:20,image:"image.jgp"}},{id:4,user:{id:40,image:null}}];
const sorted = teachers.sort((a, b) => b.user.image === null ? -1 : 1)
console.log(sorted)
This question already has answers here:
findIndex() javascript array object
(2 answers)
How do I get the index of object in array using angular?
(5 answers)
Get objects position in array
(3 answers)
Closed 1 year ago.
I have an array of object as follow :
[ {label: "<p>Opacity | %<br/></p>", customRow: false, id: 0, items: Array(13)},
{label: "Brand_hs_id", customRow: false, id: 0, items: Array(13)},
{label: "<p>PPI |</p>", customRow: false, id: 0, items: Array(13)},
{label: "Brightness | %", customRow: false, id: 0, items: Array(13)
]
I want to findIndex of object where label value matches. for example if I pass the "Brightness | %" it should return 3.
I checked
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
but not sure how to do it in array of objects .
Try something like as follows:
let val = [{id:"1002", address:"usa"},
{id:"1004", address:"canada"},
{id:"1006", address:"sweden"}];
let index = val.findIndex(x => x.address === "canada");
console.log(index); //Here index will be returned
do it like this
const array1 = [{age:5}, {age:12},{age:8} , {age:130}, {age:44}];
const isLargeNumber = (element) => element.age > 13;
console.log(array1.findIndex(isLargeNumber));
This question already has answers here:
Modify object property in an array of objects
(12 answers)
Add property to all objects in array [duplicate]
(2 answers)
How to set specific property value of all objects in a javascript object array (lodash)
(3 answers)
javascript set all values in array of object
(4 answers)
Change properties of every item in an array?
(4 answers)
Closed 3 years ago.
I created an object with javascript and want to make the "completed" property of all objects true.
codes:
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
]
I want to make the completed property of all objects true
function completeAll() {
//some codes
//I'm running with button
}
Use map.
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
];
const output = todos.map(({completed, ...rest}) => ({...rest, completed: true}));
console.log(output);
Use . notation to access and change properties in the object
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
]
todos.forEach(function(e){
e.completed=true;
})
console.log(todos)
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 6 years ago.
I want to delete object from my array posted below but nothing works. If I delete the properties it just goes to undefined. I have a recursive function that returns to me the right object. I wish to delete the second object and his children.
var data = {
parameters: [
{
id: 0,
name: "First",
value: "1",
children: []
},
{
id: 1,
name: "Second",
value: "2",
children: [
{
id: 2,
name: "Third",
value: "3",
children: []
}
]
}
],
index: 3
}
You could use Array#splice.
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
data.parameters.splice(1, 1)
// index // \\ length