Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象转换为对象数组模式_Javascript_Node.js_Arrays_Reactjs - Fatal编程技术网

Javascript 将对象转换为对象数组模式

Javascript 将对象转换为对象数组模式,javascript,node.js,arrays,reactjs,Javascript,Node.js,Arrays,Reactjs,我有一个对象,现在有数据,我想在特定的模式转换它 下面是我的目标 { _groups: ["d1830f7c-12ac-4abf-bc03-f0b70e26f8f2", "d0348b51-dcaa-2227-f0ff-912b27100aee"], _eror: "", number: "", seen_days: Infinity, // dont count this if th

我有一个对象,现在有数据,我想在特定的模式转换它

下面是我的目标

{
    _groups: ["d1830f7c-12ac-4abf-bc03-f0b70e26f8f2", "d0348b51-dcaa-2227-f0ff-912b27100aee"],
    _eror: "",
    number: "",
    seen_days: Infinity, // dont count this if this is Infinity or make add it
    address: "",
    status: "ACTIVE"
}
现在,如果我想把它转换成下面的模式

 [
        {
            "field": "status",
            "value": "ACTIVE",
            "operator": "equal"
        },
        {
            "field": "_groups",
            "value": "d1830f7c-12ac-4abf-bc03-f0b70e26f8f2",
            "operator": "equal"
        },
        {
            "field": "_groups",
            "value": "d0348b51-dcaa-2227-f0ff-912b27100aee",
            "operator": "equal"
        }
]







const convert = (obj) => {
  const arr = [];
  obj._groups.forEach((el) => {
    arr.push({
      field: "_groups",
      value: el,
      operator: "equal",
    });
  });
  console.log(obj)
  
  var key = Object.keys(obj);
  var value = obj[key];
  arr.push({
    field: value,
    value: obj.status,
    operator: "equal",
  });
  return arr;
}
目前正在尝试此功能,但未按预期工作

只包括那些有数据的字段,排除其他字段也是无限的 需要转换为数组对象的对象是动态的,每次数据都会不同,但需要转换为上面的阵列对象模式。 目的是将阵列对象的图案与为动态对象提供数据相匹配。数据会有所不同,但应将其转换为上述图案。 初始化空数组 首先从对象获取关键帧。 循环键并使用键检查对象的值是否为数组,如果为数组,则循环该键并推入数组。 如果value不是array,那么同样的事情就是使用模式格式将数组推入。 下面的代码经过测试


数据已经是复数:P数据的单数是datum。请包括您目前是如何解决它的。您当前的代码。还有你被困的地方。操作员的财产从哪里来?“它总是平等的还是什么?”Iwrestledaberance说。是的,它总是相等的,而且我已经用我一直试图设置的函数更新了问题。请帮助非常类似我的代码,我们得到了类似的想法:这是真的,开发人员也这么认为。
var sample = {
      _groups: ["d1830f7c-12ac-4abf-bc03-f0b70e26f8f2", "d0348b51-dcaa-2227-f0ff-912b27100aee"],
      _eror: "",
      number: "",
      seen_days: Infinity,
      address: "",
      status: "ACTIVE"
    }

let targetArray = [];
const keys = Object.keys(sample)
  .filter(v => !!sample[v] && sample[v] !== Infinity);

keys.forEach(key => {
  if (Array.isArray(sample[key])) {
    sample[key].forEach((sampleKey => {
      targetArray.push({
        field: key,
        value: sampleKey,
        operator: "equal"
      })
    }))
  } else {
    targetArray.push(
      {
        field: key,
        value: sample[key],
        operator: "equal"
      }
    )
  }
})


console.log(targetArray)
    const x = {
    _groups: ["d1830f7c-12ac-4abf-bc03-f0b70e26f8f2", "d0348b51-dcaa-2227-f0ff-912b27100aee"],
    _eror: "",
    number: "",
    seen_days: Infinity, // dont count this if this is Infinity or make add it
    address: "",
    status: "ACTIVE"
}
const result = []
createObj = (item,value) => {
           const temp = { }
            temp["field"] = item
            temp["value"] =  value
            temp["operator"] = "equal"
            result.push(temp)
}

Object.keys(x).forEach(item => {

     if(x[item] && x[item] !== Infinity){
        if(Array.isArray(x[item])){
           
          x[item].forEach(record => {
            createObj(item, record)
          })
          
     } else {
           createObj(item, x[item])
        }
    
     }
     
})

console.log(result)