Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中排序之前排除特定数组对象_Javascript_Jquery_Html - Fatal编程技术网

在javascript中排序之前排除特定数组对象

在javascript中排序之前排除特定数组对象,javascript,jquery,html,Javascript,Jquery,Html,我想知道如何导出(而不是删除)特定对象,然后在javascript中对嵌套数组对象进行排序, 下面是对数组对象进行排序的函数。我需要排除没有amount属性的对象,然后在javascript中对只有amount的对象进行排序,并映射该对象(需要使用exluded obj和sorted obj) 您可以首先根据每个元素是否具有amount属性,将myList筛选为两个列表: const includesAmount = myList.filter(item => item.hasOwnPro

我想知道如何导出(而不是删除)特定对象,然后在javascript中对嵌套数组对象进行排序, 下面是对数组对象进行排序的函数。我需要排除没有amount属性的对象,然后在javascript中对只有amount的对象进行排序,并映射该对象(需要使用exluded obj和sorted obj)


您可以首先根据每个元素是否具有
amount
属性,将
myList
筛选为两个列表:

const includesAmount = myList.filter(item => item.hasOwnProperty('amount'));
const excludesAmount = myList.filter(item => !item.hasOwnProperty('amount'));
includesAmount.sort(...)

const finalArray = [...includesAmount, ...excludesAmount];
这使得两次遍历
myList
,但您可以通过迭代
myList
并将每个元素推送到其各自的数组中,在一次遍历中完成此操作。

您可以使用创建带有/不带属性的数组。然后对所需数组进行排序。最后,他们喜欢以下方式:

let providerList=[{id:“transferwise”,amount:“1000”},{id:“wordlremit”,amount:“4000”},{id:“instarem”,amount:“3000”},{country:“Singapore”,scn:“SG”},{country:“India”,scn:“IN”};
让amount=providerList.filter(item=>item.hasOwnProperty('amount')).sort((a,b)=>a.amount-b.amount);
让notAmount=providerList.filter(item=>!item.hasOwnProperty('amount');
var res=notAmount.concat(金额)

控制台日志(res)
我不知道为什么上面的答案必须使用
hasOwnProperty()

只需简单地检查该属性是否存在,它就更简短、可读性更强:

sortAndFilterProviders() {
  const noAmountList = myList(item => !item['amount']);
  const amountList = myList(item => item['amount']);

  amountList.sort( (a, b) => {
    const a1 = a.amount, 
    b1 = b.amount;
    if (a1 == b1) {
      return 0;
    };
    return a1 > b1 ? 1 : -1;
   });

   console.log(amountList); // check if it is sorted
   return [... noAmountList, amountList];
 }

我需要排除没有amount属性的对象,然后对只有amount的对象进行排序…不太清楚。您期望的产量是多少?@Mamun预期产量:国家:新加坡,印度排序金额:Transferwise,Instarem,WorldTransmit谢谢您的及时回复,但我也必须使用地图中的exlcude值,在filteredlist中,我将只有已排序的对象,但我需要排序以及排除对象,因为我需要使用映射函数。我相信这将过滤掉所有可能导致意外和难以调试的错误值(0、空字符串等)behavior@miyu啊,那是真的。。!对不起,你说得对
expected output
  country: Singapore, India
  Sorted Amount : Transferwise , Instarem, Worldremit
const includesAmount = myList.filter(item => item.hasOwnProperty('amount'));
const excludesAmount = myList.filter(item => !item.hasOwnProperty('amount'));
includesAmount.sort(...)

const finalArray = [...includesAmount, ...excludesAmount];
sortAndFilterProviders() {
  const noAmountList = myList(item => !item['amount']);
  const amountList = myList(item => item['amount']);

  amountList.sort( (a, b) => {
    const a1 = a.amount, 
    b1 = b.amount;
    if (a1 == b1) {
      return 0;
    };
    return a1 > b1 ? 1 : -1;
   });

   console.log(amountList); // check if it is sorted
   return [... noAmountList, amountList];
 }