简体   繁体   English

Javascript根据另一个值在json中获取值

[英]Javascript get value in json based on another value

I have a json similar to this one 我有一个与此类似的json

 {      
    "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. 现在我需要从对象中获取所有月份的所有Value ,因为您可以看到我可能有更多具有相同月份名称的对象。 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? 我越接近创建2个数组,它们的月份列表和1个值的值,但是我被困住了,有人可以引导我找到正确的路径吗?

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 income (I already have it) and the second the total income for these months, so it's like this: ["January", "February", "March"..] and the second one [1500, 2000, 300...] 所需的输出将是得到一个像["January"=>1500, "February"=>2000...]这样的数组,或者有2个数组,其中1个是有收入的月份列表(我已经有了)第二个是这些月的总收入,所以是这样的: ["January", "February", "March"..]和第二个[1500, 2000, 300...]

You can use the function Array.prototype.reduce to sum each Value by month. 您可以使用Array.prototype.reduce函数按月对每个Value求和。

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

You can do 你可以做

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

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. 如果我对您的理解正确,则可以使用js数组的map函数将每个对象映射到其Value。

let arr = [...];
console.log(arr.map(item => item.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. 尽管您似乎不太清楚此处尝试的内容,但是还是可以读取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. 希望能有所帮助,祝您有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM