This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
JavaScript - get array element fulfilling a condition
(10 answers)
How to find first element of array matching a boolean condition in JavaScript?
(14 answers)
Closed 2 years ago.
I have array
Game: [
{ game: 'SS', status: 2 },
{ game: 'CD', status: 2 },
{ game: 'AS', status: 2 },
{ game: 'SM', status: 2 },
{ game: 'GTA', status: 1 },
]
how can I find the GTA's status is equal to 1 using find method in js?
Game.find(game => game.status===1)
You can use filter method:
const games = Game.filter(gta => gta.status == 1 );
Related
This question already has answers here:
Why does Array.prototype.push return the new length instead of something more useful?
(6 answers)
javascript push returning number instead of object [duplicate]
(1 answer)
Pushing objects in an array only returns last object pushed
(5 answers)
How to use Array.push and return the pushed item?
(3 answers)
Closed 2 years ago.
So, I was building a simple rest api on node, I fixed the problem, but I was just curious as to why I even get a number 4 to begin with? You'll know what I mean when you look at the code, It's just a small snippet of code that I'm confused about.
main.js
const people = [
{ id: 1, firstName: "Daniel"},
{ id: 2, firstName: "Erika" },
{ id: 3, firstName: "Christian"},
];
let person = people.push({ id: people.length + 1, firstName: "Mark"})
If I console.log(person) I get 4 as a value. I mean I understand that If I console.log(people) I will get what I added, but I'm just curious as to why when I console.log(person) I get a value of 4?
Ciao, as #VLAZ said, Array.push returns the new length of your people array.
If you want to get the last person inserted in people array you could do:
const people = [
{ id: 1, firstName: "Daniel"},
{ id: 2, firstName: "Erika" },
{ id: 3, firstName: "Christian"},
];
let person = people[people.push({ id: people.length + 1, firstName: "Mark"}) - 1];
console.log(person)
-1 because the 4th element of the array is the object in position 3
This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 3 years ago.
I need to sort list of items by its priority(ascending order) ?
var priorityIssuesList = [
{ name: "Danger", status: "RED", priority:4},
{ name: "Moderate", status: "BLUE", priority:2},
{ name: "Safe", status: "GREEN",priority:1},
{name: "Warning", status: "Yellow",priority:3}
]
sorting should be in ascending order or status
[
{"name":"Safe","status":"GREEN","priority":1},
{"name":"Moderate","status":"BLUE","priority":2},
{"name":"Warning","status":"Yellow","priority":3},
{"name":"Danger","status":"RED","priority":4}
]
what is the best way to do this?
try Array.sort() functionality
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for ascending order
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for descending order
console.log(priorityIssuesList);
This question already has answers here:
JavaScript merging objects by id [duplicate]
(18 answers)
Closed 6 years ago.
I have an object called probe. It has a property called sensors which is an array.
var probe = {
sensors = [
{ id: 1, checked: false },
{ id: 2, checked: true },
{ id: 3, checked: false },
... //more sensors
],
... //other properties
}
I have separate array which has an updated list of sensors like below.
var updatedSensors = [
{ id: 1, checked: true },
{ id: 3, checked: true }
];
I want to update the sensors array in the probe object from the values in the updatedSensors. How would I do that?
This can be easily achieved by using a couple of for-loops. But for-loops are not the pretty way of iterating in JavaScript, so I was wondering how to do this the preferred way.
Edit:
The objects in the updatedSensors is a subset of objects in probe.sensors. In other words, updatedSensors does not have all the objects (ids) that are there in the probe.sensors, but probe.sensors has all the objects (ids) that are in the updatedSensors.
Thanks.
Try this bit of code:
probe.sensors.map(function (sensor) {
// Loop through updated sensors & match sensor.id so we can reassign val
updatedSensors.map(function (f) {
if (f.id == sensor.id) {
sensor.checked = f.checked
}
})
return sensor;
})
This question already has answers here:
loop and push object into an array
(3 answers)
Closed 7 years ago.
I'm new to JavaScript. Here is the piece of code I'm trying to understand. I'm incrementing i value changing the name and id in img then pushing it to array data. As show in output, value is same throughout the loop. why isn't name and id changing with i. I'm unable to figure it out. Need help?
my code is
var data=[]
var img={}
for(var i =0 ;i<5;i++){
img.name="i"+i;
img.id=i;
data.push(img);
}
console.log(data);
ouput is :
[ { name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 },
{ name: 'i4', id: 4 } ]
try this code please it worked and tested :
var data=[];
for(var i =0 ;i<5;i++){
var img={};
img.name="i"+i;
img.id=i;
data.push(img);
}
console.log(data);
This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 8 years ago.
I am deleting an element from an array using delete
I ave this object:
obj = {
Variables: [ {
Name: "test",
Type: "Int",
Control: "U",
Value: "123"
}, {
Name: "ftr",
Type: "DateTime",
Control: "UA",
Value: "123123"
}, {
Name: "wertwe",
Type: "Int",
Control: "SA",
Value: "435345"
} ]
};
using this code:
delete data["Variables"][2];
Now obj contains the value:
{"Variables":[{"Name":"test","Type":"Int","Control":"U","Value":"123"},{"Name":"ftr","Type":"DateTime","Control":"UA","Value":"123123"},null]}
Is there any way to delete an element without a null value appearing in the object?
You punctured a hole in the array. You will need to use splice to remove the hole. I found the dupe after writing this answer so I'm going to vote to close with the Q/A where the example is.