从数组列表更改为对象javascript

时间:2017-07-13 10:21:53

标签: javascript arrays

我的代码有问题,因为当我增加数组中的元素数量时,它只运行最后添加的3个元素而不是完整列表我不知道我的代码中有什么问题。请帮忙!

from __future__ import absolute_import
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'newstudio.settings')

from django.conf import settings
from celery import Celery

app = Celery('newstudio',
    backend='amqp',
    broker='amqp://guest@127.0.0.1//')

# This reads, e.g., CELERY_ACCEPT_CONTENT = ['json'] from settings.py:
app.config_from_object('django.conf:settings')

# For autodiscover_tasks to work, you must define your tasks in a file called 'tasks.py'.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print("Request: {0!r}".format(self.request))

4 个答案:

答案 0 :(得分:1)

这能为您提供所需的输出吗?

var array = [
  ['make', 'Ford'],
  ['model', 'Mustang'],
  ['year', '1964'],
  ['make', 'Honda'],
  ['model', 'CRV'],
  ['year', '2000']
];

function fromListToObject(data) {
  var cars = [];
  for (var i = 0; i < data.length; i++) {
    var car = {};
    car[data[i][0]] = data[i++][1];
    car[data[i][0]] = data[i++][1];
    car[data[i][0]] = data[i][1];
    cars.push(car);
  }
  return cars;
}

var result = fromListToObject(array);
console.log(result);

更新:版本2

var array = [
  ['make', 'Ford'],
  ['model', 'Mustang'],
  ['year', '1964'],
  ['make', 'Honda'],
  ['model', 'CRV'],
  ['year', '2000']
];

function fromListToObject(data) {
  var cars = [];

  var car = {};
  data.forEach(function(item) {
    if (item.length < 2)
      return;

    var key = item[0];

    // the car already has the key, so this must be new
    // add the car to the list and create a new one.
    if (car.hasOwnProperty(key)) {
      cars.push(car);
      car = {};
    }

    car[key] = item[1];
  });

  // add the last car in
  cars.push(car);
  return cars;
}

var result = fromListToObject(array);
console.log(result);

答案 1 :(得分:1)

您必须将数据放入对象数组中。我将原始数组块分成当时键大小的片段,然后处理这些块并为它们创建单独的对象。请注意,如果原始数组包含不相等的块(例如缺少键),则下面将失败,因此您需要为此添加一个检查或确保原始数组始终是正确的:

let array = [
  ['make', 'Ford'],
  ['model', 'Mustang'],
  ['year', '1964'],
  ['make', 'Honda'],
  ['model', 'CRV'],
  ['year', '2000']
];

function toArrOfObj(array) {
  let keys = [...new Map(array).keys()];
  let i, j, res = [], chunk = keys.length;
  for (i = 0, j = array.length; i < j; i += chunk) {
    let tmp = array.slice(i, i + chunk);
    let obj = {};
    for (let [k, v] of tmp) {
      obj[k] = v;
    }
    res.push(obj);
  }
  return res;
}

console.log(toArrOfObj(array));

答案 2 :(得分:0)

&#13;
&#13;
var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', '1964'],['make', 'Honda'], ['model', 'CRV'], ['year', '2000']];


function fromListToObject(array) {  
  var cars = [];
    for(var j = 0; j < array.length/3; j++){ 
      var obj = {};
      for (var i = j; i <= j+3; i++) { 
        var arr1 = array[i];
        obj[arr1[0]] = arr1[1];
      }
      cars.push(obj)
    }
  return cars;
}

var output = fromListToObject(array);
console.log(output);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

返回完整对象

{ 
'0': { make: 'Ford', model: 'Mustang', year: '1964' },
'1': { make: 'Honda', model: 'CRV', year: '2000' }
}

&#13;
&#13;
var array = [
  ['make', 'Ford'],
  ['model', 'Mustang'],
  ['year', '1964'],
  ['make', 'Honda'],
  ['model', 'CRV'],
  ['year', '2000']
];

function fromListToObject(array) {
  var obj = {};
  var child_obj = {};
  var objindex = 0;
  var index = 0;
  while(array.length) {
    while(array.length && index < 3){
      var arr1 = array.shift();
      child_obj[arr1[0]] = arr1[1];
      index++;
    }
    index = 0;
    obj[objindex]= child_obj;
    child_obj = {};
    objindex++;
  }
  return obj;
}

var result = fromListToObject(array);
console.log(result);
&#13;
&#13;
&#13;