从for循环创建javascript对象

时间:2018-11-08 23:59:21

标签: javascript jquery

我想创建一个如下所示的可排序javascript对象:

myArray = [
  {name: "Jones, James", ef: "35", mem: "2018.12.10"},
  {name: "Smith, Paul", ef: "35", mem: "2018.09.12"},
  {name: "Washington, George", ef: "35", mem: "2018.08.16"}
];

我还计划按名称对myArray()进行排序。

myArray.sort(function(a, b){
  return a.name - b.name;
});

JAVASCRIPT:

function store() {
  var myArray = [];

  var i;
  for (i = 0; i < 10; i++) {
    NAME = document.getElementById('P' + i).value;
    EF = document.getElementById('E' + i).value;
    MEM = document.getElementById('M' + i).value;

    // This code doesn't seem to add records to myArray()

    myArray.push({
      name: 'NAME',
      ef: 'EF',
      mem: 'MEM'
    });
  }

  console.log(myArray.length);
}

store();

我对myArray进行了排序:

0: {name: "Abercrombie, Bill", rank: "1537", uscf: "9999", ef: "3", exp: "2019.01.12", …}
1: {name: "Baken, Clif", rank: "1802", uscf: "9999", ef: "3", exp: "2019.09.18", …}
2: {name: "Kukla, Ollie", rank: "2014", uscf: "0920", ef: "", exp: "2019.08.12", …}
3: {name: "Lincoln, Abraham", rank: "2023", uscf: "0119", ef: "", exp: "2019.09.06", …}
4: {name: "Washington, George", rank: "1563", uscf: "9999", ef: "3", exp: "2019.01.16", …}

如何遍历已排序的myArray()以填充表单?我想我需要这样的东西:

console.log(myArray.length);

for (i = 1; i <= myArray.length; i++) {
// where id "P1" is the name input field in the form which we are populating
document.getElementById("P" +i).value = myArray[name];

}

3 个答案:

答案 0 :(得分:1)

代码存在一些问题:

  • 未声明变量NAMEEFMEM

    这意味着these are implicit globals(!),您的代码可能会有意想不到的副作用。

    解决方案:使用varlet甚至更好的const在块范围内声明这些变量。

  • 您是推送字符串,而不是变量

    myArray.push({name: 'NAME'})是指将字符串值'NAME'作为name的值。

    解决方案::删除引号,使用name: NAME,或者甚至更好,将变量更改为name并使用ES2015 shorthand notation

    myArray.push({name, ef, mem});

  • prefer to use const if you are not reassigning variables. Use let in all other cases

    与使用旧的var相比,这使您的代码更加健壮,因为它们的怪癖更少。

示例:

function store() {
  const myArray = [];

  for (let i = 0; i < 10; i++) {
    // Replaced getElementByID since it's not relevant
    const name = 'some name';
    const ef = 'some string';
    const mem = 'some value';

    // This code doesn't seem to add records to myArray()

    myArray.push({name, ef, mem});
  }

  console.log(myArray.length);
}

store();

答案 1 :(得分:1)

由于您使用的是jQuery,为什么不这样构建myArray

for (n = 1; n <= 3; n++) {
  myArray.push({
    name: $('#P' + n).val(),
    ef: $('#E' + n).val(),
    mem: $('#M' + n).val()
  });
}

用于对myArray :(对MDN referencethis answer的信用)

myArray.sort((a, b) => {
  let nameA = a.name.toUpperCase();
  let nameB = b.name.toUpperCase();
  return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
});

然后将myArray的值放回相应的input字段中:

myArray.forEach((item, i) => {
  let n = i + 1;
  $('#P' + n).val(item.name);
  $('#E' + n).val(item.ef);
  $('#M' + n).val(item.mem);
  // ... do the same for other fields
});

答案 2 :(得分:0)

两个步骤:

1-制作Array []

2-使用排序功能对其项目进行排序

请记住,localStorage设置并获取了字符串,因此使用JSON.parse()方法进行解析

排序功能:(此属性按字母顺序排序)

Array.prototype.sort()

演示:

let result = array.sort((a.name, b.name) => a.name - b.name)