Deleting an element from a JavaScript array [duplicate] - javascript

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.

Related

Check if an array of object is exactly equal to another array that contains one of its property [duplicate]

This question already has answers here:
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 1 year ago.
I have these 2 arrays
The first one is fixed and does not change and contains the id of the 2nd array:
fixed=["123","456","789"]
The second can change
variableArray=[{name:"Joe",id:"123"},{name:"Joe",id:"456"},{name:"Joe",id:"789"}]
I want to return true if, even if there were some changes at the end the variable array is the same length and contains exactly the same keys of the "fixed"
NOT VALID:
fixed=["123","456","789"]
variableArray=[{name:"Joe",id:"456"},{name:"Joe",id:"789"}]
return false because is missing the id "123" (and the length is also different so is excluded by default)
NOT VALID:
fixed=["123","456","789"]
variableArray=[{name:"Joe",id:"123"},{name:"Joe",id:"456"},{name:"Joe",id:"001"}]
this will return false because, even if contains 3 elements as there are in the "fixed" is missing the id "789" and have another "001" instead
as #mplungjan mentiond, you can use Every:
let fixed = ["123", "456", "789"];
let variableArray1 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "789"
}];
let variableArray2 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "001"
}]
let containsAll1 = variableArray1.every(elem => fixed.includes(elem.id));
let containsAll2 = variableArray2.every(elem => fixed.includes(elem.id));
console.log(containsAll1, containsAll2);

Multiple Arrays to Single Flatterned Array [duplicate]

This question already has answers here:
How can I create every combination possible for the contents of two arrays?
(16 answers)
Cartesian product of multiple arrays in JavaScript
(35 answers)
Closed 1 year ago.
Ive got 3 parent arrays that could sometimes be different lengths which are called Options
Each Option has values array which can also be different lengths
Example of this would be something like this
[
{
name: "Option 1",
values: ["Blue","Red","Orange"]
},
{
name: "Option 2",
values: ["Small","Medium","Large"]
},
{
name: "Option 3",
values: ["Cotton","Satin"]
}
]
The outcome should generate an array like this
[
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Small/Satin",
price: "10.00"
},
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Medium/Satin",
price: "10.00"
},
]
And So on.
Ive tried mapping through the options but knowing the size of the multiple arrays is what I couldnt figure out
SOLUTION
I managed to get a solution
product.options.reduce(
(a, b) => a.flatMap((x) => b.values.map((y) => [...x, y])),
[[]]
);

Group and filter a list of objects in java script [duplicate]

This question already has answers here:
Most efficient method to groupby on an array of objects
(58 answers)
Closed 1 year ago.
The list of object is
list= [{app: "a1", company: "20", permission: "All"},
{app: "a1", company: "21", permission: "download"},
{app: "a2", company: "20", permission: "search"}]
Then I need to filter above list as
{
"a1":{20:["All"],
21:["download"]},
"a2": {20:["search"]}
}
I'm using ES6. Thanks for your concern :)
I should notice first, that Stackoverflow is resource that help in problem solving, it should not solve your tasks for you, it should help find problems in your solution.
list.reduce((acc, item) => {
if (!acc.hasOwnProperty(item.app)) {
acc[item.app] = {}
}
if (!acc[item.app].hasOwnProperty(item.company)){
acc[item.app][item.company] = []
}
acc[item.app][item.company].push(item.permission)
return acc
},{})

Referencing an Objects data into another object -> object.property.OTHEROBJECT.PROPERTY.length [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I have 2 objects, player and rooms
I have a ul tag of which i would like to populate the li depending on how many options there are and depending on where the player is.
I have the following
let player = {
name: "",
location: "start"
}
let rooms = {
start: {
name: "outside",
test: "testing",
options:{
option1: "Option 1",
option2: "OPtion 2"
}
},
end:{
name: "outside",
test: "testing",
options:{
option1: "start",
option2: "end"
}
}
}
for (let key in rooms.start.options){
let option = document.createElement("li");
option.innerHTML = (rooms.(player.location).options[key]);
optionsList.appendChild(option)
console.log();
}
I am unsure how to reference the player location in the last few lines that so I can have either:
option.innerHTML = (rooms.start.options[key]); or
option.innerHTML = (rooms.end.options[key]);
some people are advising to use arrays but I like to reference such as player.name, player.location, etc
Try This way
using Bracket notation
for (let key in rooms.start.options){
console.log(rooms[player.location].options[key]);
}
Hope it helps

What's the JavaScript way of updating an array of objects from another array? [duplicate]

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;
})

Categories

Resources