0

I have an array which holds information about availability and some other info about products. Instead of returning the all Data in Array i want to only return docs in array where the avail = true and then return all sorted by the id.

[
    {
        "id": 5,
        "avail": true,
        "name": "product A",
        "qty": 18
    }, {
        "id": 9,
        "avail": false,
        "name": "product B",
        "qty": 0
    }, {
        "id": 7,
        "avail": true,
        "name": "product C",
        "qty": 3
    }, {
        "id": 1,
        "avail": true,
        "name": "product D",
        "qty": 47
    }, {
        "id": 4,
        "avail": false,
        "name": "product E",
        "qty": 0
    }
]
1
  • 1
    When you see that three people answer within 3 minutes of your question, you know that you have asked something you could have googled in less than 3 minutes. (Instead of answering, those 3 should really have voted to have this question closed as duplicate, or at least link to a dupe). Commented Jun 17, 2019 at 19:18

4 Answers 4

2
const result = original.filter(x => x.avail);
result.sort((a, b) => a.id - b.id);

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

Comments

1

You can use filter and sort to accomplish this:

var arr = [
    {
        "id": 5,
        "avail": true,
        "name": "product A",
        "qty": 18
    }, {
        "id": 9,
        "avail": false,
        "name": "product B",
        "qty": 0
    }, {
        "id": 7,
        "avail": true,
        "name": "product C",
        "qty": 3
    }, {
        "id": 1,
        "avail": true,
        "name": "product D",
        "qty": 47
    }, {
        "id": 4,
        "avail": false,
        "name": "product E",
        "qty": 0
    }
];

var newArr = arr.filter(a => {
    return a.avail;
}).sort((a, b) => {
    return a.id > b.id;
});

console.log(newArr)

Comments

1

First you can get only the records having avail: true by calling Array.prototype.filter on the data array. Next, sort those results using Array.prototype.sort and comparing the id value of each array element.

Here's an example:

const getAvailableProducts = allProducts => allProducts.filter(el => el.avail).sort((a, b) => a.id - b.id);

const available = getAvailableProducts([{
  "id": 5,
  "avail": true,
  "name": "product A",
  "qty": 18
}, {
  "id": 9,
  "avail": false,
  "name": "product B",
  "qty": 0
}, {
  "id": 7,
  "avail": true,
  "name": "product C",
  "qty": 3
}, {
  "id": 1,
  "avail": true,
  "name": "product D",
  "qty": 47
}, {
  "id": 4,
  "avail": false,
  "name": "product E",
  "qty": 0
}]);

console.log('The following products are available:');
console.log(available);

Comments

0

const data = [{
  "id": 5,
  "avail": true,
  "name": "product A",
  "qty": 18
}, {
  "id": 9,
  "avail": false,
  "name": "product B",
  "qty": 0
}, {
  "id": 7,
  "avail": true,
  "name": "product C",
  "qty": 3
}, {
  "id": 1,
  "avail": true,
  "name": "product D",
  "qty": 47
}, {
  "id": 4,
  "avail": false,
  "name": "product E",
  "qty": 0
}];

const filteredData = data.filter(x => x.avail).sort((a, b) => a.id - b.id);
console.log(filteredData);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.