js 实现数组对象转成一维二维数组

Posted 不想掉头发啊!!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 实现数组对象转成一维二维数组相关的知识,希望对你有一定的参考价值。

一维数组实现

const obj = [
    
        name: '张飒',
        age: 13,
        phone: 123
    ,
    
        name: '凯苏',
        age: 15,
        phone: 963
    ,
    
        name: 'Jk',
        age: 16,
        phone: 852
    ,
    
        name: '库洛',
        age: 17,
        phone: 741
    
]
//  Object.entries 依据对象的键值对形成数组
// join(',') 数组转字符串
// split(",") 将字符串进行分割
const newArray = []
const arr = obj.map(item => 
    const resultString = Object.entries(item).join(',').split(",")
    /**
    resultString 输出结果
    ["name","张飒","age","13","phone","123"]
	["name","凯苏","age","15","phone","963"]
	["name","Jk","age","16","phone","852"]
	["name","库洛","age","17","phone","741"]
    */
    
    resultString.forEach(key => 
        newArray.push(key)
    )
)
console.log(newArray)  
//  ["name","张飒","age","13","phone","123","name","凯苏","age","15","phone","963","name","Jk","age","16","phone","852","name","库洛","age","17","phone","741"]

二维数组实现

const obj = [
    
        name: '张飒',
        age: 13,
        phone: 123
    ,
    
        name: '凯苏',
        age: 15,
        phone: 963
    ,
    
        name: 'Jk',
        age: 16,
        phone: 852
    ,
    
        name: '库洛',
        age: 17,
        phone: 741
    
]

const arr = obj.map(item => 
    const resultString = Object.entries(item).join(',').split(",")
    return resultString.map(item => item)
)
console.log(arr)
//  [["name","张飒","age","13","phone","123"],["name","凯苏","age","15","phone","963"],["name","Jk","age","16","phone","852"],["name","库洛","age","17","phone","741"]]

以上是关于js 实现数组对象转成一维二维数组的主要内容,如果未能解决你的问题,请参考以下文章

js 实现数组对象转成一维二维数组

一维数组对象转成二维数组

js中 把二维数组转成一维数组

PHP 将二维数组转成一维数组

vue 二维数组合并一维数组

vue 二维数组合并一维数组