remove quotes from javascript array [closed] - javascript

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 5 years ago.
Improve this question
I have a javascript with the unixtimestamp and the price of an item at that particular time. The timestamp is coming in string as listed below. How do I remove the double quotes from the timestamp. This is the array. I used the string replace function but not working.
["1356998400000", 222.69179362385]
["1357084800000", 209.18952317885]
["1357171200000", 211.95012017103]
["1357257600000", 200.15913266219]
["1357344000000", 215.58462758679]

var arr = [["1356998400000", 222.69179362385],
["1357084800000", 209.18952317885],
["1357171200000", 211.95012017103],
["1357257600000", 200.15913266219],
["1357344000000", 215.58462758679]];
arr.forEach(function(item){
item[0] = Number(item[0])
})
console.log(arr);
Just Use Number() to make a string containing number to number.

Something like this?
var myArr = ["1356998400000", 222.69179362385,
"1357084800000", 209.18952317885,
"1357171200000", 211.95012017103,
"1357257600000", 200.15913266219,
"1357344000000", 215.58462758679];
//check the values in the array before making changes
console.log(myArr);
var i;
for(i = 0; i < myArr.length; i++) {
if(typeof myArr[i] == "string") {
myArr[i] = parseFloat(myArr[i]);
}
}
//check the value of the array after changes
console.log(myArr);

Related

How to check the array is of how many dimension in javascript? [closed]

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 1 year ago.
Improve this question
For example I have an array as follows & expected output is given.
In javascript how can we determine dynamically how many levels are there in my array ary.
var ary = ["a","b",["c","d"],"e",["f","g",["h","i"],"j"]];
Output: 3
var ary = ["a","b",["c","d"],"e",["f","g","i"]];
Output: 2
var ary = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]];
Output: 4
Here is a reccursive function that will traverse through the depths of the array and track the maximum of it. Note that the tracking is done via properties attach to the function itself.
var ary1 = ["a","b",["c",["d"]],"e",[["f","g",["i","j"]],"k"]];
function getDimension(arr, start) {
//Attach depth tracking properties to the function
if (start){
getDimension.depth = 0;
getDimension.maxDepth = 0;
}
//Track max depth
getDimension.depth++
if (getDimension.depth > getDimension.maxDepth)
getDimension.maxDepth++;
//Manage recursion
for (let element of arr)
if (element instanceof Array)
getDimension(element);
getDimension.depth--;
//In first level at this point
if (getDimension.depth === 0)
return getDimension.maxDepth;
}
let d = getDimension(ary1, true);
console.log(d);

Data in Array is Incomplete [closed]

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 2 years ago.
Improve this question
Data in Array is Incomplete
it get only last item.
I need to get all item.
how to fix this?
source code
let GetProvinceWithSumCovid19 = [];
for (let index = 0; index < GetProvince.length; index++) {
GetProvinceWithSumCovid19 = dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1);
GetProvinceWithSumCovid19.push({
value: GetProvince[index].sumCovid19
})
};
console.log(GetProvinceWithSumCovid19);
The first line in the for loop overwrites GetProvinceWithSumCovid19
There's not a lot of information to go off of here, but looking at the second image of the output, I believe you're trying to do something like this:
const getProvinceWithSumCovid19 = GetProvince.map((item, index) => {
return {
[dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1)],
value: GetProvince[index].sumCovid19
}
});
console.log(getProvinceWithSumCovid19);
Instead of assigning to GetProvinceWithSumCovid19 variable, push to it.
var GetProvinceWithSumCovid19 = [];
for (let index = 0; index < GetProvince.length; index++) {
GetProvinceWithSumCovid19.push(dataFromAmCharts.filter(x => x.name.indexOf(GetProvince[index].providersEN) > -1));
GetProvinceWithSumCovid19.push({
value: GetProvince[index].sumCovid19
})
};
console.log(GetProvinceWithSumCovid19);
The output structure would be like:
[[{id:, name:},{id:, name:}], {value:2}, [{id:, name:}], {value:1}]
But I really doubt whether this is the structure you want. The required output structure is not clear in your question.

How to search in localstorage for specific word and get entire key and value? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
On click i created localstorage item "storageKey__list" and value is "example.com"
I need to get all item keys and values ends with "__list" and then get entire key and value.
Result must be:
"storageKey__list , example.com"
You could grab the entries in localStorage using Object.entries() and then use .filter() to obtain only the entires ending in "__list" by using .endsWith():
Object.entries(localStorage).filter(([key]) => key.endsWith('__list'));
Output:
[["storageKey__list", "example.com"]]
A more browser friendly version of the code above could be to use the following:
Object.keys(localStorage).filter(function(key) {
return /__list$/.test(key);
}).map(function(key) {
return [key, localStorage.getItem(key)];
});
Output:
[["storageKey__list", "example.com"]]
Try this:
/** #type {[string, string][]} */
const keyValuePairs = [];
for (let i = 0, l = localStorage.length; i < l; i++) {
const key = localStorage.key(i);
if (key.endsWith("__list"))
keyValuePairs.push([ key, localStorage.getItem(key) ]);
}
console.log(keyValuePairs);

JavaScript array push not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to push a value into an array and it is giving me this error in the developer tools.
Uncaught TypeError: Cannot read property 'push' of null
Here is the code that it seems to be sticking on, word and local word were defined earlier like this.
var word = [];
var localWord = [];
function setLocalArray() {
// first get words from text field and update word array.
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
localStorage.setItem("localWord",JSON.stringify(localWord));
text2Array();
reveal();
}
localWord.push(key2); Seems to be what it is getting stuck on. I have looked at everything I can find on the push method and I can't seem to find why it is giving me this error. Help?
Here is the full code at jsfiddle http://jsfiddle.net/runningman24/jnLtpb6y/
Try this...
var localWord = new Array(); //create new array
var word = new Array();
function setLocalArray() {
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
}
I found the problem, if you look in the jsfiddle I posted I am trying to pull localWord from localStorage even though it doesn't exist and so it sets it to null. Thank you to all for the ideas and contributions.
You could try isolating the scope of your variable using the module pattern:
var arrayManager = (function () {
var word = [];
var localWord = [];
function setLocalArray() {
// first get words from text field and update word array.
word = document.getElementById("words").value.split(',');
// store word array in localStorage
for(var i=0; word.length > i; i++) {
var key2 = "part"+i;
localStorage.setItem(key2,word[i]);
localWord.push(key2);
}
localStorage.setItem("localWord",JSON.stringify(localWord));
text2Array();
reveal();
}
return {
setLocalArray:setLocalArray
} ;
}());
and the from the outside you have to simply call arrayManager.setLocalArray()

Convert string into array of int [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I want to convert "1,2,3" to [1,2,3].
But there is an exception when converting "" to array. Because I get [""]. That is not valid for my case. So I need to check is it number or String. Let see this in code
function someWayToParse(some_string) {
var final_product = [];
var tmp_array = some_string.split(',');
//if some_string == "" tmp_array will result [""];
if (tmp_array[0].length===0)
return [];
for (var item in tmp_array)
final_product.push(parseInt(tmp_array[item], 10));
return final_product;
}
var stringToParse = "1,2,3";
var array_of_ints = someWayToParse(stringToParse);
I am just looking the best way to do this in a function and avoid possible mistakes.
Please be memory efficient, for my curiosity's sake.
Smaller code for it would be:
function myConverter(string) {
if (!string) return [];
return string.split(',').map(Number);
}
console.log(myConverter('1,2,3'));

Categories