How to remove an array element from local storage? [duplicate] - javascript

This question already has answers here:
Remove array item from localstorage
(4 answers)
Closed 4 years ago.
I have an array of contacts in local storrage, and I need to remove, for example, the first element. How better to do? Is this expression correct?
localStorage.removeItem("allContacts"[0]);

localStorage contains string values. If you stringifyed an array and put it into localStorage, then you'll need to parse the array, delete the element you want, and then set the localStorage property again:
const allContacts = JSON.parse(localStorage.allContacts);
allContacts.shift();
localStorage.allContacts = JSON.stringify(allContacts);

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

JavaScript: Extract one number value out of key-value array and store them in a Variable [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last year.
I do an InfluxDB query and get exact one result row like this:
{"result":[[{"result":"_result","table":0,"_start":"2022-01-28T09:00:12.676771291Z","_stop":"2022-01-29T09:00:12.676771291Z","_measurement":"Strom.Heizung.Energie_in_der_letzten_Stunde","_value":14.683000000000117,"_time":"2022-01-29T09:00:12.676771291Z","ts":1643446812676}]],"ts":1643446812684,"error":null}
I need the value of the _value key in a variable e.g. value. In this example the 14.683000000000117
I tried it with the map-function:
value = query.result[0].map(elm => (elm._value));
but I get the value in brackets like [14.683000000000117]
How can I get the number in my value variable?
Thanks
Frank
map() returns an array, even if it's an array of length 1. So, you want to retrieve the first element of the array it returns. You can achieve that by appending [0] at the end:
value = query.result[0].map(elm => (elm._value))[0];
Learn more on accessing array elements here, in the "Accessing Array Elements" section: https://www.w3schools.com/js/js_arrays.asp

I want to restrict my array length to a specific size? [duplicate]

This question already has answers here:
Is it possible to create a fixed length array in javascript?
(14 answers)
Closed 3 years ago.
For example, I have an array = []. I want to restrict the array length. How should I initialize it? Is there anything for restricting the size?in Javascript i cant make it? i have a JSON data with 250 length,i want them to restrict to first 20 values?
Define array size. Example:
var arr = new Array(5);

Push data into a certain position in an array [duplicate]

This question already has answers here:
How to push to an array in a particular position?
(4 answers)
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
How do I push some data into a certain place in an array?
I am trying the code below:
Keys[1].push({ Keys: "Test" });
But it doesn't seem to work as expected
The method you are looking for is splice
arrayObject.splice(index,0,{Keys:"Test"});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Categories

Resources