我们可以基于一个字段来组织Json数据吗?

hc8w905p  于 2023-02-06  发布在  其他
关注(0)|答案(5)|浏览(127)

我在每个JSON对象中有一个公共字段,即字段A。并且我有依赖于来自服务的数据的变量字段,即(变量字段A变量字段B)。
我有一个JSON数据在下面的格式:

[{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

我需要基于公共字段(这里是字段A)将可变字段放在一起,这样一个公共字段的所有可变字段都可以在一个对象中使用。我如何将JSON转换为这样的内容?

[{
    "Field A": "ABC",
    "Variable Field A": "66",
    "Variable Field B": "-729"
},
{
    "Field A": "DEF",
    "Variable Field A": "70",
    "Variable Field B": "334"
},
{
    "Field A": "GHI",
    "Variable Field A": "135",
    "Variable Field B": "962"
},
{
    "Field A": "JKL",
    "Variable Field A": "19",
    "Variable Field B": "241
}]
kqqjbcuj

kqqjbcuj1#

let response = array.reduce((storage,objectField)=>{
  let [ [fieldKey, fieldValue],[fieldVariableKey,fieldVariableValue]] = Object.entries(objectField);
  if(!storage[fieldKey+fieldValue] ){
    storage[fieldKey+fieldValue]=objectField
  }else{
    storage[fieldKey+fieldValue][fieldVariableKey]=fieldVariableValue
  }
  return storage
},{})

response = Object.values(response)

console.log(response)

这段代码考虑到了任何可能的字段。2数据可以决定有更多的字段,如字段B、C或D,所以这段代码不仅检查字段A,而且检查所有可能的字段。3我希望这能有所帮助

fv2wmkja

fv2wmkja2#

在适当的数据结构中解码json,并使用适当的匹配算法重新创建对象数组。
比如:
1.取第一个对象
1.保存字段A
1.迭代其他对象并检查域A
1.如果找到另一个字段A,则在新对象结构中添加具有找到的值的新"可变字段B"或具有找到的值的新"可变字段A"。
1.删除在原始数组中找到的项目(为了防止重复)
1.下一次迭代
如果你使用javascript来检查,hasOwnProperty("propertyName")方法检查一个字段是否存在于对象中,如果你必须从对象中获取"变量字段A"或"变量字段B",它应该是有用的。

hzbexzde

hzbexzde3#

这是一个很好的数组缩减函数的应用程序。

const data = [] // your JSON data
data.reduce((acc,item)=>({
  ...acc,
  [item['Field A']]: {...acc[item['Field A']],...item}
}),{})

结果是:

{
  ABC: {
    'Field A': 'ABC',
    'Variable Field A': '66',
    'Variable Field B': '-729'
  },
  DEF: {
    'Field A': 'DEF',
    'Variable Field A': '70',
    'Variable Field B': '334'
  },
  GHI: {
    'Field A': 'GHI',
    'Variable Field A': '135',
    'Variable Field B': '962'
  },
  JKL: {
    'Field A': 'JKL',
    'Variable Field A': '19',
    'Variable Field B': '241'
  }
}
u7up0aaq

u7up0aaq4#

你可以做这样的事情

const group = (data, field) => Object.values(data.reduce((res, item) => {
  const existing = res[item[field]] || {}
  
  return {
    ...res,
    [item[field]]: {...existing, ...item}
  }

}, {}))

const data = [{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

const result = group(data, 'Field A')

console.log(result)
pokxtpni

pokxtpni5#

我复制粘贴并修改了这个网站上的一个答案。非常神秘,但它首先创建一个由"字段A"分组的对象,然后将其扁平化为一个数组。

var arr = get_data();

var result = (Object.values(arr.reduce(function(r, a) {
  var A = a["Field A"]
  r[A] = r[A] || {};
  for (var key in a) {
    r[A][key] = a[key]
  }
  return r;
}, {})).flat())

console.log(result);

function get_data() {
  return [{
      "Field A": "ABC",
      "Variable Field A": "66"
    },
    {
      "Field A": "DEF",
      "Variable Field A": "70"
    },
    {
      "Field A": "GHI",
      "Variable Field A": "135"
    },
    {
      "Field A": "JKL",
      "Variable Field A": "19"
    },
    {
      "Field A": "ABC",
      "Variable Field B": "-729"
    },
    {
      "Field A": "GHI",
      "Variable Field B": "962"
    },
    {
      "Field A": "DEF",
      "Variable Field B": "334"
    },
    {
      "Field A": "JKL",
      "Variable Field B": "241"
    }
  ];
}
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

相关问题