String as a Single value and Array as multiple value handling [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 5 years ago.
Improve this question
If I select one value the I am getting value is like
val : "ABC"
But if I select multiple value my val is comming in array
val : ["ABC","DEF"].
Now because of this problem is neighther I can work on for single value nor multiple. Can anybody tell me how to handle this.
eg:
var myObj = {
data : "M1",
val : "Abc"
}
After adding another value it become
var myObj = {
data : "M2",
val : Abc,Def
}

function handleVal(val){
if ("string" === typeof val){
console.log("Do sth with string");
} else if (Array.isArray(val)){
console.log("Do sth with array")
}
}
handleVal("ABC");
handleVal([1,2])

Related

React Redux is creating unnecessary indexes [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 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.

I need help using localStorage [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 1 year ago.
Improve this question
I have tried multiple codes but none of them have worked. I am trying to get the player's score or "dogs"(with the variable e) to save across sessions. Here's my code https://jsfiddle.net/Zontoon/yxkzd4cb/13/.
var e = 0; //is updated as the user clicks and buys, and I need to save the user's score.
https://jsfiddle.net/ChrisWong/qwf4ey7m/6/
// init or get score from localStorage
var e = localStorage.getItem('dogs') || 0;
e = parseInt(e);
document.getElementById("var").innerHTML = "Dogs: " + e;
...
function myfunction() {
e = e + buttonvalue;
// update the element and the score
updac();
...
}
...
// update the element and the score
function updac() {
document.getElementById("var").innerHTML = "Dogs: " + e;
// save to localStorage
localStorage.setItem('dogs', e);
}
Window.localStorage has the details and I suggest you to read it to understand how to use it.

I need to sort it out this quiz [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 3 years ago.
Improve this question
my code is not working at all
I need to solve this quiz
question is write convertToString as function !
this function should convert to string from parameter
ex )
let output = convertToString(120);
console.log(output); // --> '120'
let output2 = convertToString('hello');
console.log(output2); // --> 'hello'
let output3 = convertToString(true);
console.log(output3); // --> 'true'
this is what I wrote
function convertToString(anything) {
if (typeof anything === 'number' && typeof anything === 'boolean') {
let ret = anything.toString()
} else {
return anything;
}
return ret1;
}
convertToString(120);
The easiest way to convert anything is by making + operation with ""
function convertToString(anything) {
return "" + anything
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
console.log(convertToString(null));
console.log(convertToString(undefined));
Zero checks necessary.
function convertToString(val) {
return String(val);
// or return val.toString();
// or return '' + val;
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));

Test the return value of my function correctly at the time of return [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 6 years ago.
Improve this question
I can not test the return value of my function correctly at the time of return.
My exercise is this: I must compare two arrays and return true if they have at least one identical element but I need some assistance in figuring out what is wrong with my code:
function duplicateElements(m, n){
function test (element){
return n.includes(element);
}
return m.filter(test) != [] ? true:false;
}
You have to test the length property of the return value.
function duplicateElements(m, n) {
function test(element) {
return n.includes(element);
}
return m.filter(test).length > 0 ? true : false;
}

async eachseries how pass var after finish loop [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 7 years ago.
Improve this question
I want to pass var number to callback, after loop.
For example Invenotry array has 25 items.
So after this 25 steps I want to pass number into callback (inCb), because I need to check number value.
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if(sth == othersthm)
{
number++;
}
}, function(numb) {
check number value and do sth
});
You can just use number:
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if (sth == othersthm) {
number++;
}
}, function() {
//check number value and do sth
console.log(number);
});

Categories