0

I am reading checkbox name and value. If there are two checkboxes if part gets executed, if single checkboox then else block will get execute.

if (Array.isArray(b.name)) {
  b.options = b.name.map(function(v, i) {
    return [v, b.value[i]];
  });
} else {
  b.options = b.name.map(function(v, i) {
    return [v, b.value[i]];
  });
}

For example: enter image description here

In the above code if block will get executed only when there are multiple value and it works fine because we have an Array.

name = Spine and value = Spine Value But in below scenario:

enter image description here

Here else part will get execute from the above snippet. But here it is returning an error saying that b.name.map is not a function.

How do I convert this string into an array similar to if block.

Tried approached so far in else block:

b.options = $.each(function(v, i) {
  return [b.name, b.value];
});


$.each({name: b.name, value: b.value}, function(k, v) {
  b.options = (k + "" + v);
});
2
  • Error is correct, String doesn't have .map() method. You need to handle else section of if block. Better See: How to create a Minimal, Complete, and Verifiable example. Commented Jul 3, 2018 at 7:35
  • I tried converting this sting value into key value pair but not working. Let me update the question with tried approached in else block. Commented Jul 3, 2018 at 7:39

2 Answers 2

1

You can combine both cases by concatenating your b.name value with an empty array:

b.options = [].concat(b.name).map(function(v,i) { 
    return [v, b.value[i]]; 
});

in that way, you will get an array from b.name when is a string and also when it is an array already. You may need to handle the case when b or b.name is undefined though. Or you can use more general zip function:

var zip = function(arr1, arr2) {
  var a = [].concat(arr1);
  var b = [].concat(arr2);
  var len = Math.min(a.length, b.length)

  return a.slice(0, len).map(function(item, i){
      return [a[i], b[i]];
  });
}

b.options = zip(b.name, b.value);

Mind in that case that the returned array will have the length of the shortest array.

Sign up to request clarification or add additional context in comments.

2 Comments

This works perfect but when I enter name as MONDAY and value as START_WEEK ...it IS displaying MONDAY as name correctly but attribute value="s" instead of value="START_WEEK".
I guess this is happening because in that case, b.value is a string. You can use the same trick with the empty array on b.value, or better use some more general 'zip' funcition like the one in my edited answer.
1

if b.name has length attribute,you can use 'call'

if(Array.isArray(b.name)){
   b.options = Array.prototype.map.call(b.name,function(v,i) { 
            return [v, b.value[i]]; 
   })
}else{
   b.options = Array.prototype.map.call(b.name,function(v,i) { 
            return [v, b.value[i]]; 
   })
}

2 Comments

if block is perfect for multiple name values but single name and value it is returning below error jquery-3.2.1.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in Spine
If I enter name as MONDAY it is adding M as checkbox , O as checkbox and so on instead MONDAY as checkbox

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.