如何切片或限制动态嵌套数组nodejs

How to slice or limit dynamic nested array nodejs

如何对嵌套数组中的数据进行切片? 我想将它与分页一起使用来限制显示的数据。 我已经尝试使用 object key 但只限制嵌套数组而不显示父数据

我正在尝试像这样使用对象键。输出是正确的,但没有显示 data1、data2 等父级

  dataJson.map(d => {
    for(var i=0;i<Object.keys(d).length;i++){
      let rev = d[Object.keys(d)].slice(1 * 2 - 2, 1 * 2);
      console.log(rev);
   }
  })

输出

    {
      id: 1,
      name: "John" 
    },
    {
      id: 2,
      name: "James" 
    }

    {
      id: 1,
      name: "May" 
    },
    {
      id: 2,
      name: "Joe" 
    }

单个对象数组中的结构数据json

 "data": [
    {
      "data 1": [
        {
          id: 1,
          name: "John" 
        },
        {
          id: 2,
          name: "James" 
        },
        {
          id: 3,
          name: "James" 
        }
      ]
    },
    {
      "data 2": [
        {
          id: 1,
          name: "May" 
        },
        {
          id: 2,
          name: "Joe" 
        },
        {
          id: 3,
          name: "Din" 
        }
      ]
    },
    data 3,
    many data others
 ]

我想这样显示(限制数据并显示嵌套数组中的 2 个数据) 带函数 slice

 "data": [
    {
      "data 1": [
        {
          id: 1,
          name: "John" 
        },
        {
          id: 2,
          name: "James" 
        }
      ]
    },
    {
      "data 2": [
        {
          id: 1,
          name: "May" 
        },
        {
          id: 2,
          name: "Joe" 
        }
      ]
    },
 ]

这是您的要求

    var array = [];
var data = []
var data1 = [{ name: 's' }, { name: 'v' }, { name: 'c' }];
var data2 = [{ name: 's' }, { name: 'v' }, { name: 'c' }];

array['data'] = data;
data['data 1'] = data1;
data['data 2'] = data2;

for (var i = 1; i < 3; i++) {
    array.data['data ' + i].splice(2, 1)

}
console.log(array);

输出:

[
  data: [
    'data 1': [ [Object], [Object] ],
    'data 2': [ [Object], [Object] ]
  ]
]

查看更多关于 Object.entries

let data = [{ "data 1": [{ id: 1, name: "John" }, { id: 2, name: "James" }, { id: 3, name: "James" }] }, { "data 2": [{ id: 1, name: "May" }, { id: 2, name: "Joe" }, { id: 3, name: "Din" }] },]
let out = data.map(v => {
    let obj = {}
    for (let [k, arr] of Object.entries(v))
        obj[k] = arr.slice(0, 2);

    return obj
});
console.log(out);