最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript get value in json based on another value - Stack Overflow

programmeradmin4浏览0评论

I have a json similar to this one

 {      
    "id": "1",
    "month": "January",
    "type": "inc",
    "Value": "780.00",
    "year": "2018",
  },

 {      
    "id": "2",
    "month": "January",
    "type": "inc",
    "Value": "80.00",
    "year": "2018",
  },
 {      
    "id": "3",
    "month": "February",
    "type": "inc",
    "Value": "100.00",
    "year": "2018",
  },...

Now I need to get all the Value from the object for all the months, as you can see I may have more objects with the same month name. The closer I got to was creating 2 arrays 1 with the list of Months and 1 with the value but I got stuck, can someone lead me to the correct path?

The desired output would be to get an array like that ["January"=>1500, "February"=>2000...] or have 2 arrays, 1 with the list of months where there is ine (I already have it) and the second the total ine for these months, so it's like this: ["January", "February", "March"..] and the second one [1500, 2000, 300...]

I have a json similar to this one

 {      
    "id": "1",
    "month": "January",
    "type": "inc",
    "Value": "780.00",
    "year": "2018",
  },

 {      
    "id": "2",
    "month": "January",
    "type": "inc",
    "Value": "80.00",
    "year": "2018",
  },
 {      
    "id": "3",
    "month": "February",
    "type": "inc",
    "Value": "100.00",
    "year": "2018",
  },...

Now I need to get all the Value from the object for all the months, as you can see I may have more objects with the same month name. The closer I got to was creating 2 arrays 1 with the list of Months and 1 with the value but I got stuck, can someone lead me to the correct path?

The desired output would be to get an array like that ["January"=>1500, "February"=>2000...] or have 2 arrays, 1 with the list of months where there is ine (I already have it) and the second the total ine for these months, so it's like this: ["January", "February", "March"..] and the second one [1500, 2000, 300...]

Share Improve this question edited Nov 2, 2018 at 14:50 loliki asked Nov 2, 2018 at 14:42 lolikiloliki 9572 gold badges16 silver badges39 bronze badges 1
  • 3 Post the desired output and what you've tried so far. – Ele Commented Nov 2, 2018 at 14:44
Add a ment  | 

6 Answers 6

Reset to default 5

You can use the function Array.prototype.reduce to sum each Value by month.

let arr =  [{          "id": "1",    "month": "January",    "type": "inc",    "Value": "780.00",    "year": "2018",  }, {          "id": "2",    "month": "January",    "type": "inc",    "Value": "80.00",    "year": "2018",  }, {          "id": "3",    "month": "February",    "type": "inc",    "Value": "100.00",    "year": "2018",  }],
    result = arr.reduce((a, {month, Value}) => {
      a[month] = (a[month] || 0) + +Value;
      return a;
    }, Object.create(null));
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

I actually can barely understand what you would like to achieve. Please provide some example. If I understood you correctly, you can use map function of js array to map each object to its Value.

let arr = [...];
console.log(arr.map(item => item.Value));

You can do

    var fabuaryDate = yourdata
.filter(function(data) { return data.month == "February" })
.map(function(x){return {value: x.Value} })

To get result in following format :

{
  jan : [1,2,3],
  feb : [3,4,5,6],
  april : [3,4,5]
}

do this :

var output = {}
arr.forEach(element => {
   if(!output[element.month]){
      output[month] = new Array();
   }
   output[month].push(element.value);
});

You can iterate the object and fill an array with the values of the field you want to extract, like so:

const data = [ {      
    "id": "1",
    "month": "January",
    "type": "inc",
    "Value": 780.00,
    "year": "2018",
  },
  {      
    "id": "2",
    "month": "January",
    "type": "inc",
    "Value": 80.00,
    "year": "2018",
  },
  {      
    "id": "3",
    "month": "February",
    "type": "inc",
    "Value": 100.00,
    "year": "2018",
  }];
  
let dataArray = data.reduce((accum, d) => {
  if(!accum[d.month]) accum[d.month] = 0;
  accum[d.month] += d.Value;
  return accum;
},{});
console.log(dataArray);

Although you don't seem to be clear enough with what have you tried here is an example of what you could do in order to read all the values inside the json.

function myFunction(item) {
   console.log(item.month + " with the value " + item.Value)
}

var jsonArray = [{"id": "1","month": "January", "type": "inc", "Value": "780.00", "year": "2018" }, { "id": "2",      "month": "January",  "type": "inc",   "Value": "80.00", "year": "2018"  }, { "id": "3", "month": "February",      "type": "inc", "Value": "100.00", "year": "2018" }];

jsonArray.forEach(myFunction);

Since you're working with an array of objects you must access to each of the objects in the array and then get the attribute that you require.

Hope this help, have a great day.

发布评论

评论列表(0)

  1. 暂无评论