.sort() 在数组上不起作用 ( javascript)

我正在尝试通过首先创建对象来使数据更易于管理,从而将“新发货”数组与当前库存数组合并。因此,任何相同的物品都会添加到库存中的任何现有物品中。


.sort 运行,但 flat 似乎不执行任何操作。我怀疑存在某种与我如何制作数组和弄乱索引有关的问题?


function updateInventory(arr1, arr2) {

    let invObj = {}

    let updateObj = {}

    let result = []


    arr1.forEach( x => invObj[x[1]] = x[0])

    arr2.forEach( x => updateObj[x[1]] = x[0])


    for(let key in updateObj) {

        if (invObj[key]) {

            invObj[key] += updateObj[key]

        } else {

            invObj[key] = updateObj[key]

        }

    }


    result =  Object.keys(invObj).map(key=>[invObj[key],key])

    .sort((a,b)=>{

    // attempting to sort inventory alphabetically here as required by my course's test

        return a[1] - b[1]

    })


    return result

}

var curInv = [

    [21, "Bowling Ball"],

    [2, "Dirty Sock"],

    [1, "Hair Pin"],

    [5, "Microphone"]

];


var newInv = [

    [2, "Hair Pin"],

    [3, "Half-Eaten Apple"],

    [67, "Bowling Ball"],

    [7, "Toothpaste"]

];


console.log(updateInventory(curInv, newInv));


ITMISS
浏览 382回答 4
4回答

侃侃无极

在对字符串值进行排序时,应使用该方法。localeComparefunction updateInventory (arr1, arr2) {  let invObj = {};  let updateObj = {};  let result = [];  arr1.forEach(x => invObj[x[1]] = x[0]);  arr2.forEach(x => updateObj[x[1]] = x[0]);  for (let key in updateObj) {    if (invObj[key]) {      invObj[key] += updateObj[key];    } else {      invObj[key] = updateObj[key];    }  }  result = Object.keys(invObj)    .sort((a, b) => a.localeCompare(b))    .map(key => [invObj[key], key]);  return result;}var curInv = [  [21, 'Bowling Ball'],  [2, 'Dirty Sock'],  [1, 'Hair Pin'],  [5, 'Microphone']];var newInv = [  [2, 'Hair Pin'],  [3, 'Half-Eaten Apple'],  [67, 'Bowling Ball'],  [7, 'Toothpaste']];console.log(  updateInventory(curInv, newInv));

翻过高山走不出你

您是否希望数组与第一个实例中的数据格式相同?function updateInventory(arr1, arr2) {    let invObj = {}    let updateObj = {}    let result = []    arr1.forEach( x => invObj[x[1]] = x[0])    arr2.forEach( x => updateObj[x[1]] = x[0])    for(let key in updateObj) {        if (invObj[key]) {            invObj[key] += updateObj[key]        } else {            invObj[key] = updateObj[key]        }    }        return invObj;}var curInv = [    [21, "Bowling Ball"],    [2, "Dirty Sock"],    [1, "Hair Pin"],    [5, "Microphone"]];var newInv = [    [2, "Hair Pin"],    [3, "Half-Eaten Apple"],    [67, "Bowling Ball"],    [7, "Toothpaste"]];console.log(updateInventory(curInv, newInv));

jeck猫

我会创建一个查找对象并保留对数组项的引用。在循环访问当前项目后,我将循环访问新项目。检查它是否存在并更新计数。如果不存在,则将物料添加到库存中。var curInv = [    [21, "Bowling Ball"],    [2, "Dirty Sock"],    [1, "Hair Pin"],    [5, "Microphone"]];var newInv = [    [2, "Hair Pin"],    [3, "Half-Eaten Apple"],    [67, "Bowling Ball"],    [7, "Toothpaste"]];// make a look up object to reference by the keyvar lookup = curInv.reduce( (obj, item) => ({ ...obj, [item[1]]: item }), {})// loop over the new inventory and add it onnewInv.forEach((item) => {  // check to see if we have the item  var key = item[1]  var exisiting = lookup[key]  // if exists add it  if (exisiting) {    exisiting[0] += item[0]  } else {    // new item    // add to our look up table in case it repeats    lookup[key] = item    // add it to the inventory list    curInv.push(item)  }})console.log(curInv)

白衣染霜花

对字符串进行排序时,它有点复杂,因为您还必须考虑大小写。这是我在js中制作的表格中的一个片段,希望它有所帮助。您也可以像 taplar 所说的那样使用 localecompare。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sort(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function(a,b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = a[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = b[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a < b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (a > b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0; // Equal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript