0

i have a bunch of options in this select, each with values like:

context|cow
context|test
thing|1
thing|5
thing|27
context|beans 

while looping through the options, I want to build an array that checks to see if keys exist, and if they don't they make the key then append the value. then the next loop through, if the key exists, add the next value, comma separated.

the ideal output would be:

arr['context'] = 'cow,test,beans';
arr['thing'] = '1,5,27';

here's what i have so far, but this isn't a good strategy to build the values..

function sift(select) {
        vals = [];
        $.each(select.options, function() {
            var valArr = this.value.split('|');
            var key = valArr[0];
            var val = valArr[1];
            if (typeof vals[key] === 'undefined') {
                vals[key] = []; 
            }
            vals[key].push(val);
        });
        console.log(vals);
}
2

6 Answers 6

1

Existing code works by changing

 vals=[];

To

 vals={};

http://jsfiddle.net/BrxuM/

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

Comments

1
function sift(select) {
    var vals = {};//notice I made an object, not an array, this is to create an associative array
    $.each(select.options, function() {
        var valArr = this.value.split('|');
        if (typeof vals[valArr[0]] === 'undefined') {
            vals[valArr[0]] = ''; 
        } else {
            vals[valArr[0]] += ',';
        }
        vals[valArr[0]] += valArr[1];
    });
}

Here is a demo: http://jsfiddle.net/jasper/xtfm2/1/

Comments

1

How about an extensible, reusable, encapsulated solution:

function MyOptions()
{
  var _optionNames = [];
  var _optionValues = [];
  function _add(name, value)
  {
    var nameIndex = _optionNames.indexOf(name);
    if (nameIndex < 0)
    {
      _optionNames.push(name);
      var newValues = [];
      newValues.push(value);
      _optionValues.push(newValues); 
    }
    else
    {
      var values = _optionValues[nameIndex];
      values.push(value);
      _optionValues[nameIndex] = values;
    }
  };
  function _values(name)
  {
    var nameIndex = _optionNames.indexOf(name);
    if (nameIndex < 0)
    {
      return [];
    }
    else
    {
      return _optionValues[nameIndex];
    }
  };
  var public = 
  {
    add: _add,
    values: _values
  };

  return public;
}

usage:

var myOptions = MyOptions();
myOptions.add("context", "cow");
myOptions.add("context","test");    
myOptions.add("thing","1");    
myOptions.add("thing","5");    
myOptions.add("thing","27");    
myOptions.add("context","beans");    

console.log(myOptions.values("context").join(","));
console.log(myOptions.values("thing").join(","));

working example: http://jsfiddle.net/Zjamy/

Comments

0

I guess this works, but if someone could optimize it, I'd love to see.

function updateSiftUrl(select) { var
    vals = {};
    $.each(select.options, function() {
        var valArr = this.value.split('|');
        var key = valArr[0];
        var val = valArr[1];
        if (typeof vals[key] === 'undefined') {
            vals[key] = val;    
            return;
        }
        vals[key] = vals[key] +','+ val;
    });
    console.log(vals);
}

Comments

0

Would something like this work for you?

$("select#yourselect").change(function(){
    var optionArray =
        $(":selected", $(this)).map(function(){
            return $(this).val();
        }).get().join(", ");
});

If you've selected 3 options, optionArray should contain something like option1, option2, option3.

Comments

-1

Well, you don't want vals[key] to be an array - you want it to be a string. so try doing

        if (typeof vals[key] === 'undefined') {
            vals[key] = '; 
        }
        vals[key] = vals[key] + ',' + val;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.