1

I would like to know how to exlcude (not remove) particular object and then sort nest array object in javascript, Below function that sorts array object. i need to exclude the objects that is not having amount property and then sort the object having only amount in javascript and map the object(need to use exluded obj and sorted obj).

this.providerList = [{id:"transferwise", amount:"1000"}, {id:"wordlremit", amount:"4000", {id:"instarem", amount:"3000"}, {country: "Singapore", scn: "SG"}, {country: "India", scn: "IN"}];
 sortAndFilterProviders() {
    let myList = [];
    myList = [...this.providerList];
    myList.push.apply(myList, this.apiproviderdata.apiproviders);
    // var mergeList = myList.concat(this.query);
    // console.log(mergeList);
    myList.sort(function (a, b) {
      var a1 = a.amount, b1 = b.amount;
      if (a1 == b1) return 0;
      return a1 > b1 ? 1 : -1;
    });
    this.providerList = [...myList];
    return this.providerList;
  }
expected output
  country: Singapore, India
  Sorted Amount : Transferwise , Instarem, Worldremit
2
  • i need to exclude the objects that is not having amount property and then sort the object having only amount...is not quite clear. What is the output you are expecting? Commented Mar 25, 2019 at 4:19
  • @Mamun the expected output: country: Singapore, India Sorted Amount : Transferwise , Instarem, Worldremit Commented Mar 25, 2019 at 4:36

3 Answers 3

1

You can first filter myList into two lists based on whether each element has the amount property:

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

const finalArray = [...includesAmount, ...excludesAmount];

This makes two passes through myList, but you can do it in one pass by iterating through myList and pushing each element to its respective array.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for prompt reply, but i have to use exlcude value in map also, in filteredlist i will have only sorted objects but i need to sort as well as have excluded objects since i need to use map function
0

You can use filter() to create the arrays with/without the property. Then sort the desired array. Finally concat() them like the following way:

let providerList = [{id:"transferwise", amount:"1000"}, {id:"wordlremit", amount:"4000"}, {id:"instarem", amount:"3000"}, {country: "Singapore", scn: "SG"}, {country: "India", scn: "IN"}];

let amount = providerList.filter( item => item.hasOwnProperty('amount')).sort((a,b)=> a.amount - b.amount);
let notAmount = providerList.filter( item => !item.hasOwnProperty('amount'));
var res = notAmount.concat(amount)
console.log(res);

Comments

0

I am not sure why the above answers must make use of hasOwnProperty().

It is simply shorter and more readable to simply check if that property exist:

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];
 }

2 Comments

I believe that will filter out all falsy values (0, empty strings, etc) which might result in unexpected and hard to debug behavior
@miyu Ah that is true..! Sorry about that, you are actually right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.