【问题标题】:Javascript reformat or map an array of objects to a new array of new objects? like reformating itJavascript重新格式化或将对象数组映射到新对象数组?就像重新改造它
【发布时间】:2021-03-12 12:00:17
【问题描述】:

我是数组和对象的新手,所以我整天都在做这个任务我有一个 JSON 文件或数组语言(JavaScript),我也在 StackOverflow 上搜索,但不能很好地理解它们:

[
  { "date": "1959:01", this"1": 138.89, "2": 139.39, "3": 139.74, "4": 139.69, "5": 140.68, "6": 141.17 },
  { "date": "1959:07", "1": 141.7, "2": 141.9, "3": 141.01, "4": 140.47, "5": 140.38, "6": 139.95 },
  { "date": "1960:01", "1": 139.98, "2": 139.87, "3": 139.75, "4": 139.56, "5": 139.61, "6": 139.58 },
  { "date": "1960:07", "1": 140.18, "2": 141.31, "3": 141.18, "4": 140.92, "5": 140.86, "6": 140.69 },
  { "date": "1961:01", "1": 141.06, "2": 141.6, "3": 141.87, "4": 142.13, "5": 142.66, "6": 142.88 },
  { "date": "1961:07", "1": 142.92, "2": 143.49, "3": 143.78, "4": 144.14, "5": 144.76, "6": 145.2 },
  { "date": "1962:01", "1": 145.24, "2": 145.66, "3": 145.96, "4": 146.4, "5": 146.84, "6": 146.58 },
  { "date": "1962:07", "1": 146.46, "2": 146.57, "3": 146.3, "4": 146.71, "5": 147.29, "6": 147.82 },
  { "date": "1963:01", "1": 148.26, "2": 148.9, "3": 149.17, "4": 149.7, "5": 150.39, "6": 150.43 },
  { "date": "1963:07", "1": 151.34, "2": 151.78, "3": 151.98, "4": 152.55, "5": 153.65, "6": 153.29 },
  { "date": "1964:01", "1": 153.74, "2": 154.31, "3": 154.48, "4": 154.77, "5": 155.33, "6": 155.62 },
  { "date": "1964:07", "1": 156.8, "2": 157.82, "3": 158.75, "4": 159.24, "5": 159.96, "6": 160.3 },
  { "date": "1965:01", "1": 160.71, "2": 160.94, "3": 161.47, "4": 162.03, "5": 161.7, "6": 162.19 },
  { "date": "1965:07", "1": 163.05, "2": 163.68, "3": 164.85, "4": 165.97, "5": 166.71, "6": 167.85 },
  { "date": "1966:01", "1": 169.08, "2": 169.62, "3": 170.51, "4": 171.81, "5": 171.33, "6": 171.57 },
  { "date": "1966:07", "1": 170.31, "2": 170.81, "3": 171.97, "4": 171.16, "5": 171.38, "6": 172.03 }
]

这些就像公司在指定年份的收入,例如在 1959:01 年初,从 1 到 6 是一年的前 6 个月,同年 1956:07 但在“:07”中存在差异是同一年最后 6 个月(1 到 6)的值,所以我想用最容易理解的代码来制作每个月的输出如下所示。

  [
  {
"year": 1996,
"month": 1,
"value": 88512.12
  },
  {
"year": 1996,
"month": 2,
"value": 71212.12
  },
  {
"year": 1996,
"month": 3,
"value": 1212.12
  },
  {
"year": 1996,
"month": 4,
"value": 754212.12
  },{
"year": 1996,
"month": 5,
"value": 545221.12
  }
  
]

非常感谢帮助。

这是我的代码,不认为它有帮助,我认为我希望有一个新的更容易和更易读的代码(经过编辑,我认为这个比另一个更符合逻辑):

const month = {};
let i = 1
let m = 1
for (let obj of finance) {
  const year = obj["date"].split(":")[0];
  month[m] = obj[`${m}`]
  // month = {...month}
  value = month[0]
  console.log('this is the value >>>>>>>>>>' + value)
  formated[year] = {year ,month}
  // delete formated[year]["date"];
  i++
  if (m < 6 ) {m++}
}

console.log(formated,"<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>", month);

fs.writeFileSync(
  path.resolve(__dirname, "Try1.json"),
  JSON.stringify(formated)
);

【问题讨论】:

  • 请更新您的问题以包含您已经尝试过的代码以及有关该代码不适合您的任何具体问题。
  • 我认为它太乱了,我的代码根本无法理解,它真的很难、很复杂而且完全错误,所以我试图让专业人员避免所有这些混乱
  • stackoverflow.com/questions/65062973/… 为您提供了答案。你能建立在那个解决方案上吗?
  • 是的,我想要一些其他格式,这根本不是他们在工作中想要的,我希望它像本文中的那样
  • 因为你有塞尔吉奥指出的答案,所以很难想给你一个直接的答案。在您正在努力解决的另一个问题中,您误解了哪些概念?

标签: javascript arrays json object javascript-objects


【解决方案1】:

阅读这个包,它会让你的生活更轻松:use of _map - Lodash

【讨论】:

  • 谢谢,但不幸的是,这不是我想要的
【解决方案2】:

如果我正确理解您的问题,那么您可以这样做:

const financialResults = [
  { "date": "1959:01", "1": 138.89, "2": 139.39, "3": 139.74, "4": 139.69, "5": 140.68, "6": 141.17 },
  { "date": "1959:07", "1": 141.7, "2": 141.9, "3": 141.01, "4": 140.47, "5": 140.38, "6": 139.95 },
  { "date": "1960:01", "1": 139.98, "2": 139.87, "3": 139.75, "4": 139.56, "5": 139.61, "6": 139.58 },
  { "date": "1960:07", "1": 140.18, "2": 141.31, "3": 141.18, "4": 140.92, "5": 140.86, "6": 140.69 },
  { "date": "1961:01", "1": 141.06, "2": 141.6, "3": 141.87, "4": 142.13, "5": 142.66, "6": 142.88 },
  { "date": "1961:07", "1": 142.92, "2": 143.49, "3": 143.78, "4": 144.14, "5": 144.76, "6": 145.2 },
  { "date": "1962:01", "1": 145.24, "2": 145.66, "3": 145.96, "4": 146.4, "5": 146.84, "6": 146.58 },
  { "date": "1962:07", "1": 146.46, "2": 146.57, "3": 146.3, "4": 146.71, "5": 147.29, "6": 147.82 },
  { "date": "1963:01", "1": 148.26, "2": 148.9, "3": 149.17, "4": 149.7, "5": 150.39, "6": 150.43 },
  { "date": "1963:07", "1": 151.34, "2": 151.78, "3": 151.98, "4": 152.55, "5": 153.65, "6": 153.29 },
  { "date": "1964:01", "1": 153.74, "2": 154.31, "3": 154.48, "4": 154.77, "5": 155.33, "6": 155.62 },
  { "date": "1964:07", "1": 156.8, "2": 157.82, "3": 158.75, "4": 159.24, "5": 159.96, "6": 160.3 },
  { "date": "1965:01", "1": 160.71, "2": 160.94, "3": 161.47, "4": 162.03, "5": 161.7, "6": 162.19 },
  { "date": "1965:07", "1": 163.05, "2": 163.68, "3": 164.85, "4": 165.97, "5": 166.71, "6": 167.85 },
  { "date": "1966:01", "1": 169.08, "2": 169.62, "3": 170.51, "4": 171.81, "5": 171.33, "6": 171.57 },
  { "date": "1966:07", "1": 170.31, "2": 170.81, "3": 171.97, "4": 171.16, "5": 171.38, "6": 172.03 }
]

// Extract semester and year: assign 2 if entry corresponds to second semester or 1 if it corresponds to the first
// semester in the year
const extractYearAndSemester = date => ({ year: date.split(":")[0], semester: date.split(":")[1] > 6 ? 2 : 1 })

// For each entry, return an array of objects with the desired properties
// Finally, flatten the array, so you don't end up with an array of arrays
const formatResults = arr => arr.
  flatMap(entry => {
  const { year, semester } = extractYearAndSemester(entry.date)
  let semesterResults = []

  for (let key in entry) {
    if (parseInt(key)) {
      semesterResults.push({
        year,
        month: semester === 1 ? parseInt(key) : parseInt(key) + 6,
        value: entry[key]
      })
    }
  }

  return semesterResults
  })

console.log(formatResults(financialResults))

【讨论】:

  • 是的,伙计... Yeeeeaaaaah 伙计,这是 bigStack 社区伙伴干杯?
  • 如此酷的命名和非常华丽的代码,我喜欢 thaaat...
【解决方案3】:

尝试遍历数据并解析每条记录的属性,如下所示。在外循环结束时,您将在 monthData 变量中获得所需格式的数据;

var data = [
      { "date": "1959:01", "1": 138.89, "2": 139.39, "3": 139.74, "4": 139.69, "5": 140.68, "6": 141.17 },
      { "date": "1959:07", "1": 141.7, "2": 141.9, "3": 141.01, "4": 140.47, "5": 140.38, "6": 139.95 },
    ];
     var monthData = [];
    
      for (let index = 0; index < data.length; index++) {
        var item =data[index];
        var dateArray = item.date.split(":");
        var year = dateArray[0];
        var baseMonth = Number(dateArray[1]);
        for (var key in item) {
          if (item.hasOwnProperty(key)) {
              var val = item[key];
              if (String(val).indexOf(":")== -1) {
    
              monthData.push({
                year:year ,
                month:  baseMonth-1 + Number(key),
                value: val,
              });
            }
          }
        }
        
      }

【讨论】:

  • 是的,我马上试试这个,非常感谢
  • 是的,谢谢,谢谢,非常棒的 sn-p fo 代码伙伴,它工作得很好?
  • @MansouriAla 很高兴我能提供帮助。
猜你喜欢
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
相关资源
最近更新 更多