Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
Arrays 如何将节点js中的格式数组从1字符串更改为另一个对象_Arrays_Node.js_Json_Concat - Fatal编程技术网

Arrays 如何将节点js中的格式数组从1字符串更改为另一个对象

Arrays 如何将节点js中的格式数组从1字符串更改为另一个对象,arrays,node.js,json,concat,Arrays,Node.js,Json,Concat,我有这样一个json: { “地位”:1, “消息”:“确定”, “数据”:[ { “期限”:“23” }, { “期限”:“17” }, { “期限”:“37” }, { “期限”:“27,29” }, { “期限”:“33,35” } ] } 我希望结果是这样的: { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27"

我有这样一个json:

{
“地位”:1,
“消息”:“确定”,
“数据”:[
{
“期限”:“23”
},
{
“期限”:“17”
},
{
“期限”:“37”
},
{
“期限”:“27,29”
},
{
“期限”:“33,35”
}
]
}
我希望结果是这样的:

{ "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27" }, { "tenor": "29" }, { "tenor": "33" }, { "tenor": "35" } ] }
我所尝试的:

var arrayCoba = [];
var array1 = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }

for(var i = 0; i<array1.data.length; i++){
    var string = array1.data[i].tenor;
    var substring = ",";

    if(string.includes(substring) == true){
        var tenor = array.data[i].tenor;
        var tenorArr = tenor.split(',');
        var dataTenor = tenorArr.map(tenor => ({ tenor }));
        arrayCoba.push(dataTenor);
    }
 }

 var dataHasil = array1.data.concat(arrayCoba);

 return res.json({status:1,message:'ok',data:dataHasil}); 


有人能帮我吗?谢谢..

在将结果放入JSON.stringify函数后,您从mongodb获得结果,而不是像“{”name“:5,“value“:6}”这样获得结果
var dataTenor=tenorar.map(tenor=>({tenor}))返回tenor对象的数组。
arrayCoba.push(dataTenor)在其他数组中添加数组,为此,您将在数组中获得一个数组

使用->
arrayCoba.push(…dataTenor)

更新:与以前的解决方案相比,具有递归更通用

const data = [{
        "tenor": "23"
    },
    {
        "tenor": "17"
    },
    {
        "tenor": "37"
    },
    {
        "tenor": "27,29"
    },
    {
        "tenor": "33,35"
    }
]

/**
 *
 * @param {*} fn function callback
 * @param {*} v string[] or string
 * @param {*} attr string
 * @param {*} substring string
 */
const processDeepObject = (fn, v, attr, substring) => {
    return typeof v === 'string' ? {
        [attr]: v
    } : v[attr] ? fn(v[attr].split(substring), attr, substring) : [];
}

/**
 * Version 1
 * @param {*} arr Array of objects
 * @param {*} attr target you want keep (for this use case tenor)
 * @param {*} substring which char to split the data (for this use case ',')
 */
const deepObjectFlatten = (arr, attr, substring) => [].concat(...arr.map((v) => processDeepObject(deepObjectFlatten, v, attr, substring)));



/**
 * Version 2 same as version 1 but compact
 * @param {*} arr Array of objects
 * @param {*} attr target you want keep (for this use case tenor)
 * @param {*} substring which char to split the data (for this use case ',')
 */
const deepObjectFlattenV2 = (arr, attr, substring) => [].concat(...arr.map((v) => {
    return typeof v === 'string' ? {
        [attr]: v
    } : v[attr] ? deepObjectFlattenV2(v[attr].split(substring), attr, substring) : [];
}));


console.log(deepObjectFlatten(data, 'tenor', ','))
console.log(deepObjectFlattenV2(data, 'tenor', ','))
/*
Output :
//v1
[ { tenor: '23' },
  { tenor: '17' },
  { tenor: '37' },
  { tenor: '27' },
  { tenor: '29' },
  { tenor: '33' },
  { tenor: '35' } ]

//v2
  [ { tenor: '23' },
  { tenor: '17' },
  { tenor: '37' },
  { tenor: '27' },
  { tenor: '29' },
  { tenor: '33' },
  { tenor: '35' } ]
 */

您可以按如下方式进行操作:

function test() {
    let obj = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }
    let res = [];
    obj.data
        .map(item => item.tenor.split(','))
        .forEach(tenors => {
            tenors.forEach(tenor => res.push({tenor}));
        });

    obj.data = res;
    return obj;
}
调用上述方法后,返回:

{"status":1,"message":"ok","data":[{"tenor":"23"},{"tenor":"17"},{"tenor":"37"},{"tenor":"27"},{"tenor":"29"},{"tenor":"33"},{"tenor":"35"}]}

您以错误的方式使用map返回的结果,您可以使用John提到的spread操作符来修复此问题

您可以这样做:

{ "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27" }, { "tenor": "29" }, { "tenor": "33" }, { "tenor": "35" } ] }
var arrayCoba=[];
var array1={“status”:1,“message”:“ok”,“data”:[{“tenor”:“23”},{“tenor”:“17”},{“tenor”:“37”},{“tenor”:“27,29”},{“tenor”:“33,35”}
对于(VarI=0;i({“期限”:期限}));
arrayCoba.push(…数据期限);
}否则{
arrayCoba.push({“tenor”:array1.data[i].tenor})
}
}
返回res.json({状态:1,消息:ok,数据:arrayCoba});

John,dacost使用了几乎相同的逻辑,之前没有看到,因此发布了此信息。

你能解释一下吗?你能给我们举个例子说明你的目的吗?我很高兴:)。
function test() {
    let obj = { "status": 1, "message": "ok", "data": [ { "tenor": "23" }, { "tenor": "17" }, { "tenor": "37" }, { "tenor": "27,29" }, { "tenor": "33,35" } ] }
    let res = [];
    obj.data
        .map(item => item.tenor.split(','))
        .forEach(tenors => {
            tenors.forEach(tenor => res.push({tenor}));
        });

    obj.data = res;
    return obj;
}
{"status":1,"message":"ok","data":[{"tenor":"23"},{"tenor":"17"},{"tenor":"37"},{"tenor":"27"},{"tenor":"29"},{"tenor":"33"},{"tenor":"35"}]}