React Redux is creating unnecessary indexes [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to get data from my server and format that data into an indexed array and storing that in my store. The problem is when I try to manually define the indexes Redux automatically nulls any indexes that were undefined. Because I have one index with a very large number Redux indexes many unnecessary items and crashes.
Image of the bug:
Image of my code:

This happens because langStrings[i].LayoutTypeId is an huge number and if you do filteredLangStrings[langStrings[i].LayoutTypeId] = [] javascript creates an array of length langStrings[i].LayoutTypeId with all the elements set to null.
To avoid this, you should change your filteredLangStrings shape in this way:
let filteredLangStringsIndex = 0;
for(let i = 0; i < langStrings.length; i++) {
if(langStrings[i].LanguageId === langId) {
let index = langStrings[i].LayoutTypeId;
if (langStrings[i].Msg !== "") {
filteredLangStrings[filteredLangStringsIndex] = {};
filteredLangStrings[filteredLangStringsIndex].index = index;
filteredLangStrings[filteredLangStringsIndex].Msg = langStrings[i].Msg; ​
​ filteredLangStringsIndex ++;
​}
​ }
}
This creates something like:
filteredLangStringsIndex = {"0": {index: 89746, Msg: "message"},
"1": {index: 54543, Msg: "another message"}, ...}
In this way you have a smaller filteredLangStringsIndex by keeping index and Msg informations.

Related

Multi-push vs. multi-map [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
I'm asked to get attributes collection out of an array object,
let a = [
{name:'aname',age:21},
{name:'bname',age:22},
{name:'cname',age:23},
{name:'dname',age:24},
{name:'ename',age:25},
{name:'fname',age:26},
{name:'gname',age:27}]
// wanted
let ok = {
names:'aname;bname;cname;dname;ename;fname;gname',
ages:'21;22;23;24;25;26;27'
}
and I got 2 ways of doing it:
alpha just using map of an array:
// alpha
let res = {
names:'',
ages:''
}
res.names=a.map(iter=>iter.name).join(';')
res.ages=a.map(iter=>iter.age).join(';')
//then return res
// ========================================================
and beta just iterate the array and append each attribute in the tabulation array:
// beta
let res = {
names:[],
ages:[]
}
a.forEach(iter=>{
res.names.push(iter.name)
res.ages.push(iter.age)
})
// then handle res's fields
ok.names = res.names.join(';')
ok.ages = res.ages.join(';')
so which way should I use to get the collection? Will alpha get slower or faster than beta when the objects in a get lots of fields(attrs)?
Both approaches are good. I'd say it depends on your personal preference what you'd want to use.
However, It seems to me that if you are aiming for performance, the following would yield better results.
let a = [
{name:'aname',age:21},
{name:'bname',age:22},
{name:'cname',age:23},
{name:'dname',age:24},
{name:'ename',age:25},
{name:'fname',age:26},
{name:'gname',age:27}]
let ok = { names: '', ages: ''}
for (let i = 0; i < a.length; i++){
const iter = a[i]
ok.names += iter.name + ";";
ok.ages += iter.age + ";";
}
ok.names = ok.names.slice(0,-1)
ok.ages = ok.ages.slice(0,-1)
console.log(ok)
This apporach eliminates the need to create new arrays or joining them (join is a heavy operation). Just create the string you want and at the end of it all, remove the one extra semicolon.
I consider that alfa is simpler and clearer for me, but I guess it is up to you...

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

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

How to declare a json object or array collection with variable size in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

Categories