Data in Array is Incomplete [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 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.

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

Does a map update automatically if the original array is updated? [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
I am creating an array in a for loop and using a map to keep track of the number of times I've used certain values (e.g. key: count; "AB": 2, "BC": 3). I've declared the empty array and created the map for that, outside the loop.
Inside the loop, I'm updating the array based on the value in the map ( if (map.get(x) < 3) {array[i] = x}).
I'm assuming the condition isn't working because I'm not updating the map, but is there a way to have the map update automatically as the array is updated?
var array = [];
var map = array.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
for (var i = 0; i < 24; i++) {
var random = Math.floor(Math.random() * 10)
if (map.get(random) < 3) {
array[i] = random
}
}

Make all numbers in array Absolute value 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 2 years ago.
Improve this question
I'm trying to make all values in the below array absolute, after trying several methods the results that appears is 5 the first element in the array. the below is the code given:
describe('absoluteValueArray', () => {
it('Gets multiple absolute values', () => {
const result = absoluteValueArray([-5,-50,-25,-568])
expect(result).toEqual([5,50,25,568])
})
})
Function I tried is the below:
const absoluteValueArray = (array) => {
var index, len;
var array = ["-5", "-50", "-25" , "-568"];
for (index = 0, len = array.length; index < len; ++index) {
let res = Math.abs(array[index]);
return res;
}
}
You approach does not work in this line and the next
let res = Math.abs(array[index]);
return res;
because you need to assign the absolute value to the array or a new array at the same index, like
resultArray[i] = Math.abs(array[index]);
and return the array after finishing the loop.
The original return inside of the loop exits the loop with the first element.
Instead, you could take Math.abs as callback for Array#map.
const absoluteValueArray = (array) => {
return array.map(Math.abs);
}
console.log(absoluteValueArray([-5, -50, -25, -568]));

remove quotes from javascript array [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 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);

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()

Categories