js:通过索引值操纵多维数组

时间:2017-08-21 18:16:55

标签: javascript jquery arrays

我需要按第一个索引的值对多维数组进行排序。 JS或jQuery解决方案,请尝试for循环,但没有成功。

我希望改造波纹管阵列

var pinData = [
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 1,<br> Event Date1</div>'],
  ['<h3>Venue Name 2</h3>', '<div class="left">Session Name 2,<br> Event Date2</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 3,<br> Event Date3</div>'],
  ['<h3>Venue Name 4</h3>', '<div class="left">Session Name 4,<br> Event Date4</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 5,<br> Event Date5</div>'],
  ['<h3>Venue Name 6</h3>', '<div class="left">Session Name 6,<br> Event Date6</div>'],
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 7,<br> Event Date7</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 8,<br> Event Date8</div>']
]

进入这个:

var pinData = [
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 1,<br> Event Date1</div><div class="left">Session Name 7,<br> Event Date7</div>'],
  ['<h3>Venue Name 2</h3>', '<div class="left">Session Name 2,<br> Event Date2</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 3,<br> Event Date3</div><div class="left">Session Name 5,<br> Event Date5</div><div class="left">Session Name 8,<br> Event Date8</div>'],
  ['<h3>Venue Name 4</h3>', '<div class="left">Session Name 4,<br> Event Date4</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 3,<br> Event Date3</div><div class="left">Session Name 5,<br> Event Date5</div><div class="left">Session Name 8,<br> Event Date8</div>'],
  ['<h3>Venue Name 6</h3>', '<div class="left">Session Name 6,<br> Event Date6</div>'],
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 1,<br> Event Date1</div><div class="left">Session Name 7,<br> Event Date7</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 3,<br> Event Date3</div><div class="left">Session Name 5,<br> Event Date5</div><div class="left">Session Name 8,<br> Event Date8</div>']
]

在这种情况下pinData[0][1] = pinData[6][1]pinData[0][1]应该成为pinData[0][1] + pinData[0][6]

地点名称3:pinData[2][1] = pinData[4][1] = pinData[7][1],因此pinData[2][2]应成为pinData[2][2] + pinData[4][2] + pinData[7][2]

对于每个场景名称,只要index2处的数据合并,就会出现更多。

有什么想法吗?

谢谢

3 个答案:

答案 0 :(得分:0)

如果顺序无关紧要,您可以将其切换为对象,其中键为[0]项:

var pinData = {
  '<h3>Venue Name 1</h3>': '<div class="left">Session Name 1,<br> Event Date1</div><div class="left">Session Name 7,<br> Event Date7</div>'
}

然后你可以检查对象是否有密钥:

if (pinData['<h3>Venue Name 1</h3>']) {

如果是,请将新数据附加到该密钥:

pinData['<h3>Venue Name 1</h3>'] += ...

否则,您可以设置值:

pinData['<h3>Venue Name 1</h3>'] = ...

同样,请记住,对象按照定义无序

答案 1 :(得分:0)

采用未排序的数组,您可能需要先对其进行排序,然后对其进行分组。这是一种方式:

&#13;
&#13;
// Data set we'll be working with
var pinData = [
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 1,<br> Event Date1</div>'],
  ['<h3>Venue Name 2</h3>', '<div class="left">Session Name 2,<br> Event Date2</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 3,<br> Event Date3</div>'],
  ['<h3>Venue Name 4</h3>', '<div class="left">Session Name 4,<br> Event Date4</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 5,<br> Event Date5</div>'],
  ['<h3>Venue Name 6</h3>', '<div class="left">Session Name 6,<br> Event Date6</div>'],
  ['<h3>Venue Name 1</h3>', '<div class="left">Session Name 7,<br> Event Date7</div>'],
  ['<h3>Venue Name 3</h3>', '<div class="left">Session Name 8,<br> Event Date8</div>']
]

// Generic function to create a series of objects
//   The object key is the venue, and the event
//   is being pushed on if it matches the current
//   group venue.
var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x[1]);
    return rv;
  }, {});
};

// Here's where we actually start working.
//
// First, we want to sort the array by index.
var sortedArray = pinData.sort(function(a, b){
  return(a[0] > b[0]);
})

// Now, with the sorted array, we'll create
//  another array, with matching els combined.
var groupedArray = groupBy(sortedArray, 0);

// Simply a code dump, do with it what you will.
var containerEl = document.getElementsByClassName("container")[0];

containerEl.innerHTML = JSON.stringify(groupedArray, null, 2);
&#13;
<div class="container">

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我设法用for循环对它进行排序。不确定它是否是最快或更好的方法,但它有效。

首先是将数据分成2个差异数组,一个用于保存场地名称,第二个用于事件。

maping第一个数组并创建一个数组,该数组在此数组中保存了场地名称的第一个外观的索引,这有助于创建循环。

就是这样:

var pinData = [
  '<h3>Venue Name 1</h3>', 
  '<h3>Venue Name 2</h3>',
  '<h3>Venue Name 3</h3>',
  '<h3>Venue Name 4</h3>',
  '<h3>Venue Name 3</h3>',
  '<h3>Venue Name 6</h3>',
  '<h3>Venue Name 1</h3>',
  '<h3>Venue Name 3</h3>',
  '<h3>Venue Name 4</h3>'
]

var indexes = pinData.map(x => pinData.indexOf(x));
console.log(indexes);

var pinData2 = [
 '<div class="left">Session Name 1,<br> Event Date1</div>',
 '<div class="left">Session Name 2,<br> Event Date2</div>',
 '<div class="left">Session Name 3,<br> Event Date3</div>',
 '<div class="left">Session Name 4,<br> Event Date4</div>',
 '<div class="left">Session Name 5,<br> Event Date5</div>',
 '<div class="left">Session Name 6,<br> Event Date6</div>',
 '<div class="left">Session Name 7,<br> Event Date7</div>',
 '<div class="left">Session Name 8,<br> Event Date8</div>',
 '<div class="left">Session Name 9,<br> Event Date9</div>'
]

var results = [];


alert(pinData.length)
for(var i=0; i<pinData.length; i++){
  results[i] = pinData[i];
  for(var j=0; j<pinData.length; j++){
        if( indexes[j] == indexes[i] ){
        results[i] += pinData2[j]
    }
  }
}

console.log(results);


var containerEl = document.getElementsByClassName("container")[0];
//containerEl.innerHTML = JSON.stringify(results);
containerEl.innerHTML = indexes;

var containerEl = document.getElementsByClassName("container")[1];
//containerEl.innerHTML = JSON.stringify(results);
containerEl.innerHTML = results;

这里有一个fiddle