0

I have a json array at smsPackageList as like:

[{"name":"Basic","min_quantity":1250,"max_quantity":2500,"validity":"2020-07-02 15:58:18","rate":0.2},{"name":"Delux","min_quantity":2501,"max_quantity":5000,"validity":"2020-07-02 15:58:18","rate":0.19},{"name":"Classic","min_quantity":50001,"max_quantity":70000,"validity":"2020-07-02 16:03:54","rate":0.18},{"name":"Business","min_quantity":70001,"max_quantity":90000,"validity":"2020-07-02 16:03:54","rate":17},{"name":"Corporate","min_quantity":90001,"max_quantity":1000000,"validity":"2020-07-02 16:05:07","rate":0.15}]

I want to get the value of the max_quantity of the last row. I am trying as follows:

    var n = smsPackageList.length;
    var minSms = smsPackageList[0].min_quantity;
    var maxSms = smsPackageList[n-1].min_quantity;

but console.log(minSms) and console.log(maxSms) produce undefined. Whats wrong in my approach, Any idea?

4
  • Can you paste the screenshot of your console. Commented Jul 2, 2020 at 16:56
  • is that a typo that you wrote smsPackageList[0].min_quantity but you actually want max_quantity? Commented Jul 2, 2020 at 17:00
  • I missed to parse the response. problem was the data format. Commented Jul 2, 2020 at 17:14
  • @AbdusSattarBhuiyan See my response below and some explanation on using an easy forEach function to avoid parsing and etc. Thanks Commented Jul 2, 2020 at 17:17

3 Answers 3

1

Try this..

var smsPackageList = [{"name":"Basic","min_quantity":1250,"max_quantity":2500,"validity":"2020-07-02 15:58:18","rate":0.2},{"name":"Delux","min_quantity":2501,"max_quantity":5000,"validity":"2020-07-02 15:58:18","rate":0.19},{"name":"Classic","min_quantity":50001,"max_quantity":70000,"validity":"2020-07-02 16:03:54","rate":0.18},{"name":"Business","min_quantity":70001,"max_quantity":90000,"validity":"2020-07-02 16:03:54","rate":17},{"name":"Corporate","min_quantity":90001,"max_quantity":1000000,"validity":"2020-07-02 16:05:07","rate":0.15}];

console.log("min_quantity : ", smsPackageList[smsPackageList.length - 1].min_quantity);
console.log("max_quantity : ", smsPackageList[smsPackageList.length - 1].max_quantity);

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

1 Comment

While this might be solution but please add some explanation how you got the results and why you choose this method. as This is bit complicated method
1
var smsPackageList = [{"name":"Basic","min_quantity":1250,"max_quantity":2500,"validity":"2020-07-02 15:58:18","rate":0.2},{"name":"Delux","min_quantity":2501,"max_quantity":5000,"validity":"2020-07-02 15:58:18","rate":0.19},{"name":"Classic","min_quantity":50001,"max_quantity":70000,"validity":"2020-07-02 16:03:54","rate":0.18},{"name":"Business","min_quantity":70001,"max_quantity":90000,"validity":"2020-07-02 16:03:54","rate":17},{"name":"Corporate","min_quantity":90001,"max_quantity":1000000,"validity":"2020-07-02 16:05:07","rate":0.15}];

var n = smsPackageList.length;
var maxSms = smsPackageList[n-1].max_quantity;
console.log(maxSms);

Produce the correct result.

1 Comment

Thanks. I forget to parse the string into json.
1

A good way to do this is use forEach function on your array data and then . and if condition on the name you are trying yo access

You can read more about forEach here

var data = [{
  "name": "Basic",
  "min_quantity": 1250,
  "max_quantity": 2500,
  "validity": "2020-07-02 15:58:18",
  "rate": 0.2
}, {
  "name": "Delux",
  "min_quantity": 2501,
  "max_quantity": 5000,
  "validity": "2020-07-02 15:58:18",
  "rate": 0.19
}, {
  "name": "Classic",
  "min_quantity": 50001,
  "max_quantity": 70000,
  "validity": "2020-07-02 16:03:54",
  "rate": 0.18
}, {
  "name": "Business",
  "min_quantity": 70001,
  "max_quantity": 90000,
  "validity": "2020-07-02 16:03:54",
  "rate": 17
}, {
  "name": "Corporate",
  "min_quantity": 90001,
  "max_quantity": 1000000,
  "validity": "2020-07-02 16:05:07",
  "rate": 0.15
}]

var min = document.getElementById('min_qt')
var max = document.getElementById('max_qt')

data.forEach(function(element){
   if (element.name === 'Corporate') {
         min.textContent = 'Min = '+element.min_quantity
         max.textContent = 'Max = '+element.max_quantity
   }
})
<div id="min_qt"></div>

<div id="max_qt"></div>

Comments

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.