Unable to delete an array index/item using delete [duplicate] - javascript

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 3 years ago.
I am using delete in order to try to delete an item on an array;
const handleRowDeletion = index => {
const inputsAdded = shareStructureInputs;
const inputs = startupFourthStepForm.shares_estructure;
delete inputs[index];
delete inputsAdded[index];
}
I can see the first array inputsAdded getting deleted at the proper index but not the second one inputs.
This is what inputsAdded returns:
[
"share-structure-input-0",
"share-structure-input-1",
"share-structure-input-2"
]
And inputs:
[
{
"name": "Name 1",
"percentage": 10
},
{
"name": "Name 2",
"percentage": 10
},
{
"name": "Name 3",
"percentage": 80
}
]
And I call the function on a map
<Button onClick={() => handleRowDeletion(index)}>
- Delete Row
</Button>

Related

Extracting specific value from json response [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I add a json value from a response to a variable and then print it this way result: ${JSON.stringify(result)}, but what i want exactly is to get only one exact value from this json
here's the json
{
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
I only want to extract lionsflag string only from the first flagname, this value is able to change any time
You need to use Object.keys() to get all the keys and then check the related value is meet your demand or not
let data = {
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
let value = "lionsflag"
data.flags.filter(d => Object.keys(d).forEach(e =>{
if(d[e] == value){
console.log(e + ";" + value)
}
}))
let prop ='flagname'
console.log(data.flags[0][prop])

Find index of an object in an arrray of objects [duplicate]

This question already has answers here:
How can I get the index of an object by its property in JavaScript?
(22 answers)
Closed 1 year ago.
I have an array that looks like this:
const arr = [
{ "-MrUU6N2pi7aCwJoqCzP": { "name": "test", "brand": "test", "id": 1 } },
{ "-MrUUB4PcPD5G45NGb-y": { "name": "test2", "brand": "test2", "id": 2 } }
]
How do I find the index of the object with the key "-MrUUB4PcPD5G45NGb-y" for example?
You can use findIndex() and look at the Object.keys() for each object in the array to see if the key you want is in that keys array
const arr = [
{"-MrUU6N2pi7aCwJoqCzP":{"name":"test","brand":"test","id":1}},
{"-MrUUB4PcPD5G45NGb-y":{"name":"test2","brand":"test2","id":2}}
],
keyWanted = "-MrUUB4PcPD5G45NGb-y",
idx = arr.findIndex(e => Object.keys(e).includes(keyWanted))
console.log('Index =', idx)

how can I retrieve specific property of an object of multiple objects [duplicate]

This question already has answers here:
Perform .join on value in array of objects
(14 answers)
Closed 1 year ago.
I need help in getting values of specific property of an array of multiple objects
I have an array like this:
[
{
"id": 1,
"name": "skill 1"
},
{
"id": 2,
"name": "skill 2"
},
{
"id": 3,
"name": "skill 3"
}]
and I want to get a string like this :
skill 1 - skill 2 - skill 3
First step is to extract name. You can use map.
elements.map(x=>x.name)
Second step is to join it to one string using join
const result = elements.map(x=>x.name).join(' - ');

How to efficiently remove nameless attribute from json object js [duplicate]

This question already has answers here:
How do I remove all null and empty string values from an object? [duplicate]
(13 answers)
Closed 1 year ago.
I have a very large array of objects and some of the objects have a nameless property. What is an efficient way to remove the property?
[
...
{
"phone_number": "***-***-****",
"category": "Abc",
"link_to_company": "www.example.com",
"email": "test#test.com",
"company_name": "test company",
"": "123", // this need to be removed
"status": 1
},
...
]
A map function should do it.
array.map(v => { delete v[""]; return v })

delete an item in an object array by id [duplicate]

This question already has answers here:
Find and remove objects in an array based on a key value in JavaScript
(14 answers)
Closed 1 year ago.
I want to delete an item based on its id.
I retrieve id from the cell where I click but I don't know how to retrieve id in the array to be able to compare it to that of the click to delete only the one selected and not all of the array, I'm not sure which method to use
My item.json
[{
"id": "e3c387fd-7cf1-4d5b-9825-cef745c0ab99",
"name": "item 1",
"fans": {},
"cell": [{
"id": "e2021621-9c74-4960-bf47-f6ad917ee40b",
"name": "cell 1 item 1",
},
{
"id": "d5129940-716c-47a3-81b5-f2c90e69b602",
"name": "cell 2 item 1",
}
]
},
{
"id": "79fe939b-4c64-4b73-bebd-6563f445920c",
"name": "item 2",
"fans": {},
"cell": [{
"id": "7b6b57c6-7b72-4a14-8932-51fc2e5f9b75",
"name": "cell 1 item 2",
},
{
"id": "b579f94f-605e-4c7a-a8c5-3aad9bfec9e2",
"name": "cell 2 item 2",
}
]
}
]
My function
trashItem(id) {
this.cellsListsData = this.cellsListsData.filter(cell => {
cell.id === id, console.log(cell.id, id);
});
}
this.cellsListsData = this.cellsListsData.filter(function(element) {
return element.id !== id;
});
or Just Get the index of it and then this.cellsListsData.splice(index, 1);
This would require traversing through the whole array and finding the index of object with the given id. Once you find the id, you can simply splice the array from that index with offset 1.
Below is a quick snippet for doing that.
var index_to_delete;
data.forEach(function(value, index) {
if (value.id == id_of_clicked_element) {
index_to_delete = index;
};
});
data.splice(index_to_delete, 1);
Note: Using filter will create a new array. So, if you are referencing the data array in some other variables, they will not get the new array. So, I recommend using the filter solution with caution. Also, some answers say to delete element at that index. This would remove that index from array and your array will be rendered inconsistent.

Categories

Resources