从字符串中分隔值

时间:2014-11-21 10:17:10

标签: javascript jquery

我有2个像这样的字符串

var a= '12,13,14,15,16';
var b='p,q,q,p,q';

我只需要这样12,15代表p  和13,14,16代表q

我如何在Jquery / javascript中执行此操作。

4 个答案:

答案 0 :(得分:2)

<击>

<击>
var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");

if(as.length == bs.length)
{
  var map = {};
  for(var i = 0; i < as.length; ++i)
  {
    var asv = as[i];
    var bsv = bs[i];
    map[asv] = bsv;
  }
  console.log(map['13']); //q
}

或:

var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");

if(as.length == bs.length)
{
  var map = {};
  as.map(function(asv,idx){
    return {asv:asv, bsv:bs[idx]};
  })
  .forEach(function(x){
    map[x.asv] = x.bsv;
  });
  console.log(map['13']); //q
}

<击>

在回答你的评论时,也许这样的事情会更好:

var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");

if(as.length == bs.length)
{
  var map = {};
  as.map(function(asv,idx){
    return {asv:asv,bsv:bs[idx]};
  })
  .forEach(function(x){
    if(!map[x.bsv])
    {
      map[x.bsv]=[];
    }
    map[x.bsv].push(x.asv);
  });
  console.log(map['q']); //["13", "14", "16"]
  console.log(map['q'].join(",")); //"13,14,16"
}

答案 1 :(得分:0)

这很简单:

var a= '12,13,14,15,16';
var b='p,q,q,p,q';

var pValues=[], qValues=[]; //where to store the results

b.split(',').forEach(function(value, index){    //split the values from the b var
    var aa=a.split(',');                        //split the values from the a var
    if(value=='p') pValues.push(aa[index]);
    if(value=='q') qValues.push(aa[index]);
});
console.log("pValues: "+pValues);
console.log("qValues: "+qValues);

答案 2 :(得分:0)

var a= '12,13,14,15,16';
var b='p,q,q,p,q';

function getRepresentative(srcA, srcB, valA)
{
  var mapIndexA = srcA && srcA.split(',').indexOf(valA);
  var mapB = srcB.split(',');


  return mapB && mapB[mapIndexA] || -1;
}

console.log(getRepresentative(a,b, '15'));

如果找不到A和B之间的相应映射,则该函数返回-1。

答案 3 :(得分:0)

以下函数接受两个字符串并用逗号分隔它们,然后逐个迭代符号 - 标记对。每当发现新符号时,它都会被添加到symbols,并且会向symbolToNumbers添加一个空数组,以便可以将此符号的数字推送到数组中。

最后,我们可以迭代symbols以显示每个符号的数字列表。此代码适用于各种符号,而不仅仅是&#39; p&#39;和&#39; q&#39;。

function findElements(numbers, symbols) {
    var numberTokens = numbers.split(','),
        symbolTokens = symbols.split(','),
        symbolToNumbers = {},
        symbols = [],
        n = numberTokens.length;
    if (n != symbolTokens.length) {
        console.log('error: the lists must have the same length');
        return;
    }
    for (var i = 0; i < n; ++i) {
        var number = numberTokens[i],
            symbol = symbolTokens[i];
        if (symbolToNumbers[symbol] === undefined) {
            symbols.push(symbol);
            symbolToNumbers[symbol] = [];
        }
        symbolToNumbers[symbol].push(number);
    }
    for (var i = 0; i < symbols.length; ++i) {
        var symbol = symbols[i],
            numbers = symbolToNumbers[symbol];
        console.log(symbol+': '+numbers.join(', '));
    }
}

var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
findElements(a, b);

请参阅在JSFiddle上运行的此代码:http://jsfiddle.net/0e1g2ryf/1/