亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 按復合鍵對 obj 進行排序

Javascript 按復合鍵對 obj 進行排序

絕地無雙 2023-09-28 16:51:53
我有一個這樣定義的對象:{        _id: "5d406a171ed43384972f04b5",        index: 0,        age: 28,        eyeColor: "brown",        name: {          first: "Myra",          last: "Navarro"        },        company: "SUSTENZA",        email: "[email protected]"      }我需要能夠對所有鍵進行搜索,即使是像name.例如,我有一個搜索字符串name.first等,它可能是更深層次的復合對象。我設法做到了這一點,但例如name.first我無法讓它發揮作用。你能幫我個忙嗎?
查看完整描述

3 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

第一個問題是 javascript 不會自動使用點訪問子項。

接下來,一旦你有了想要比較的值。

如果值是字符串,則需要使用特殊的字符串比較函數(使用 typeof 進行檢查)。或者使用 valA - valB 以便獲得 1、0 和 -1 作為用于排序的返回值。


查看完整回答
反對 回復 2023-09-28
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

我希望這就是您所需要的。該child功能可以滿足您的需求。你在評論里有解釋。


let datatable = [

  {

    _id: "5d406a171ed43384972f04b5",

    index: 0,

    age: 28,

    eyeColor: "brown",

    name: {

      first: "Myra",

      last: "Navarro"

    },

    company: "SUSTENZA",

    email: "[email protected]"

  },

  {

    _id: "5d406a170db0f4b04d9a9acf",

    index: 1,

    age: 23,

    eyeColor: "blue",

    name: {

      first: "Harriett",

      last: "Tanner"

    },

    company: "VALPREAL",

    email: "[email protected]"

  },

  {

    _id: "5d406a17e95da8ff80a759c5",

    index: 2,

    age: 39,

    eyeColor: "blue",

    name: {

      first: "Vega",

      last: "Hanson"

    },

    company: "BEDLAM",

    email: "[email protected]"

  },

  {

    _id: "5d406a175505da190e6875ec",

    index: 3,

    age: 31,

    eyeColor: "blue",

    name: {

      first: "Rosemary",

      last: "Fields"

    },

    company: "QUAILCOM",

    email: "[email protected]"

  },

  {

    _id: "5d406a17ea96044c027f4e50",

    index: 4,

    age: 27,

    eyeColor: "brown",

    name: {

      first: "Dale",

      last: "Wilkinson"

    },

    company: "QIAO",

    email: "[email protected]"

  },

  {

    _id: "5d406a17c5fff1ff6653a555",

    index: 5,

    age: 25,

    eyeColor: "blue",

    name: {

      first: "Beatrice",

      last: "Contreras"

    },

    company: "ZENOLUX",

    email: "[email protected]"

  },

  {

    _id: "5d406a17a199efcba25e1f26",

    index: 6,

    age: 34,

    eyeColor: "blue",

    name: {

      first: "Hancock",

      last: "Wynn"

    },

    company: "PLASMOS",

    email: "[email protected]"

  },

  {

    _id: "5d406a17019a2a4544a4f134",

    index: 7,

    age: 40,

    eyeColor: "blue",

    name: {

      first: "Brown",

      last: "Stanton"

    },

    company: "SNACKTION",

    email: "[email protected]"

  },

  {

    _id: "5d406a17e516dd71af8210d4",

    index: 8,

    age: 39,

    eyeColor: "blue",

    name: {

      first: "Barnes",

      last: "Dunn"

    },

    company: "PORTALINE",

    email: "[email protected]"

  },

  {

    _id: "5d406a17516936a025b73c33",

    index: 9,

    age: 34,

    eyeColor: "green",

    name: {

      first: "Blanche",

      last: "Cherry"

    },

    company: "ISOSWITCH",

    email: "[email protected]"

  },

  {

    _id: "5d406a17527a4d2c6a7897dd",

    index: 10,

    age: 33,

    eyeColor: "blue",

    name: {

      first: "Gilliam",

      last: "Farley"

    },

    company: "AMTAS",

    email: "[email protected]"

  },

  {

    _id: "5d406a175ff11478c416c30b",

    index: 11,

    age: 26,

    eyeColor: "brown",

    name: {

      first: "Laura",

      last: "Short"

    },

    company: "FISHLAND",

    email: "[email protected]"

  },

  {

    _id: "5d406a1738181b471847339a",

    index: 12,

    age: 20,

    eyeColor: "brown",

    name: {

      first: "Moreno",

      last: "Barber"

    },

    company: "KEENGEN",

    email: "[email protected]"

  },

  {

    _id: "5d406a17a6bcae6fe3ad1735",

    index: 13,

    age: 30,

    eyeColor: "brown",

    name: {

      first: "Fischer",

      last: "French"

    },

    company: "INCUBUS",

    email: "[email protected]"

  },

  {

    _id: "5d406a17600ca53e8f63f263",

    index: 14,

    age: 30,

    eyeColor: "brown",

    name: {

      first: "Donaldson",

      last: "Carr"

    },

    company: "SUNCLIPSE",

    email: "[email protected]"

  },

  {

    _id: "5d406a17530655789a27174f",

    index: 15,

    age: 35,

    eyeColor: "green",

    name: {

      first: "Sophia",

      last: "Payne"

    },

    company: "PRISMATIC",

    email: "[email protected]"

  },

  {

    _id: "5d406a175dbc687b4c7669d8",

    index: 16,

    age: 34,

    eyeColor: "green",

    name: {

      first: "Simone",

      last: "Pollard"

    },

    company: "DIGIGEN",

    email: "[email protected]"

  },

  {

    _id: "5d406a179f35ed326a6a5567",

    index: 17,

    age: 28,

    eyeColor: "green",

    name: {

      first: "Yvette",

      last: "Daugherty"

    },

    company: "CHILLIUM",

    email: "[email protected]"

  }

];


const sortAsc = true;

const sortField = "name.first";//index,eyeColor


var result = datatable.sort((a, b) => {

  let [x, z] = sortAsc ? [a, b] : [b, a];

  //console.log(x[sortField], z[sortField], x[sortField] > z[sortField] ? 1 : -1);

  //return x[sortField] > z[sortField] ? 1 : -1;

  return child(x, sortField) > child(z, sortField) ? 1: -1;

});


console.log(result);


function child(obj, str){

  //take dot-separated chain of properties' names and split them into an array

  const props = str.split('.');

  let child = obj;

  //looop through all properties' names and get the access to the following [Object] children sequentially

  props.forEach((prop)=> child = child[prop]);

  return child;

}


查看完整回答
反對 回復 2023-09-28
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

選項1:


const objRoute = sortField.split(".");

const getValue = obj => objRoute.reduce((v, c) => v[c], obj);

const result = datatable.sort((a, b) =>

  sortAsc

    ? getValue(a).localeCompare() - getValue(b).localeCompare()

    : getValue(b).localeCompare() - getValue(a).localeCompare()

);

選項2:


使用 eval,eval() 計算表達式。建議謹慎使用該功能:


const result = datatable.sort((a, b, i) => eval(`a.${sortField}`) - eval(`b.${sortField}`));



查看完整回答
反對 回復 2023-09-28
  • 3 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號