【问题标题】:arrange array to become object array base on duplicate string根据重复的字符串排列数组成为对象数组
【发布时间】:2017-11-01 12:32:02
【问题描述】:

我有数组,我想根据点之前的相同字符串将其排列到对象数组结构中,我想创建一个对象数组结构以使其可编辑,因此我可以填充数组中每个数据的值

var arr = ["data",
        "data.cell",
        "data.cell.celltiming",
        "data.cell.celltiming.0",
        "data.cell.celltiming.1",
        "data.cell.earfcn",
        "data.cell.pci",
        "data.cell.rsrp",
        "data.cell.rsrp.0",
        "data.cell.rsrp.1",
        "data.cell.rsrp.2",
        "data.cell.rsrq",
        "data.cell.rsrq.0",
        "data.cell.rsrq.1",
        "data.cell.rsrq.2",
        "data.cell.sinr",
        "data.cell.sinr.0",
        "data.cell.sinr.1",
        "data.cells",
        "data.cells.0",
        "data.cells.0.ch",
        "data.cells.0.ecno",
        "data.cells.0.r99",
        "data.cells.0.rscp",
        "data.cells.0.sc",
        "data.cells.1",
        "data.cells.1.ch",
        "data.cells.1.ecno",
        "data.cells.1.r99",
        "data.cells.1.rscp",
        "data.cells.1.sc",
        "data.cells.2",
        "data.cells.2.ch",
        "data.cells.2.ecno",
        "data.cells.2.r99",
        "data.cells.2.rscp",
        "data.cells.2.sc",
        "data.cells.3",
        "data.cells.3.ch",
        "data.cells.3.ecno",
        "data.cells.3.r99",
        "data.cells.3.rscp",
        "data.cells.3.sc",
        "data.id",
        "data.mac",
        "data.plmn",
        "data.rssi",
        "data.time",
        "deviceID",
        "time"]

我怎样才能把它安排成对象数组结构

var arr = [    "data": {
      "plmn": "",
      "id": "",
      "time": '',
      "cell": {
        "rsrp": [
          0,
          1,
          2
        ],
        "rsrq": [
          0,
          1,
          2
        ],
        "earfcn": '',
        "pci": '',
        "celltiming": [
          0,
          1
        ],
        "sinr": [
          0,
          1
        ]
      },
      "mac": ""
    },
    "time": '',
    "deviceID": ""]

我想创建一个对象数组,这样我就可以填充每个键的值

【问题讨论】:

  • 您尝试分享代码的内容
  • @Bhargav 我尝试使用 map 和 slice 但错误......并且混淆了
  • Stackoverflow 不是免费的代码编写服务。请展示您尝试过的内容。目标是帮助您修复您的代码
  • @charlietfl 我知道,但我仍然不了解 array.map(function(d,i){ var t = d.split('.') if (t...try many事情)})我卡住了你能帮我吗
  • 你问的是相当复杂而不是微不足道的

标签: javascript arrays


【解决方案1】:

我用过以下:

  1. Array.prototype.reduce(),累加到空对象{}
  2. accumulator 对象的引用 (ref),
  3. String.prototype.split() 从数组中的当前字符串中提取单个 keys(子字符串),
  4. 遍历keys 以沿着key“链”前进,

    一个。如果key 不存在于accumulator 对象中,则在该键处创建一个对象,即if (!ref[key]) ref[key] = {}

    b.通过重新设置引用(ref)继续遍历accumulator对象,即ref = ref[key]

  5. 移动到原数组中的下一个字符串

var arr = ["data",
  "data.cell",
  "data.cell.celltiming",
  "data.cell.celltiming.0",
  "data.cell.celltiming.1",
  "data.cell.earfcn",
  "data.cell.pci",
  "data.cell.rsrp",
  "data.cell.rsrp.0",
  "data.cell.rsrp.1",
  "data.cell.rsrp.2",
  "data.cell.rsrq",
  "data.cell.rsrq.0",
  "data.cell.rsrq.1",
  "data.cell.rsrq.2",
  "data.cell.sinr",
  "data.cell.sinr.0",
  "data.cell.sinr.1",
  "data.cells",
  "data.cells.0",
  "data.cells.0.ch",
  "data.cells.0.ecno",
  "data.cells.0.r99",
  "data.cells.0.rscp",
  "data.cells.0.sc",
  "data.cells.1",
  "data.cells.1.ch",
  "data.cells.1.ecno",
  "data.cells.1.r99",
  "data.cells.1.rscp",
  "data.cells.1.sc",
  "data.cells.2",
  "data.cells.2.ch",
  "data.cells.2.ecno",
  "data.cells.2.r99",
  "data.cells.2.rscp",
  "data.cells.2.sc",
  "data.cells.3",
  "data.cells.3.ch",
  "data.cells.3.ecno",
  "data.cells.3.r99",
  "data.cells.3.rscp",
  "data.cells.3.sc",
  "data.id",
  "data.mac",
  "data.plmn",
  "data.rssi",
  "data.time",
  "deviceID",
  "time"
]
var obj = arr.reduce((accumulator, currentValue, currentIndex) => {
  let ref = accumulator;
  currentValue.split(".").forEach(key => {
    if (!ref[key]) ref[key] = {}
    ref = ref[key];
  })
  return accumulator;
}, {})

console.log(JSON.stringify(obj, null, "\t"));

这可以帮助您完成大部分工作。现在你需要做的就是:

  1. 遍历对象(第一遍),用空字符串替换空对象,然后

    arr.forEach(chain => { // first pass, change empty objects to empty strings
      let ref = obj;
      chain.split(".").forEach(key => {
        if (Object.keys(ref[key]).length === 0) {
          ref[key] = ''
        } else {
          ref = ref[key]
        }
      })
    })
    
  2. 遍历对象(第二遍),检查对象的所有键是否为数字且所有值是否为空字符串,并将此对象替换为键的数组。

    function keysAreNumbersAndValuesAreEmptyStrings(object) {
      let entries = Object.entries(object);
      for (let i = 0; i < entries.length; i++) {
        let key = entries[i][0], value = entries[i][1];
        if(isNaN(Number(key)) || typeof value !== "string" || value.length !== 0) return false;
      }
      return true;
    }
    
    arr.forEach(chain => { // second pass, change objects whose keys are numbers to an array of those keys
      let ref = obj;
      chain.split(".").forEach(key => {
        if (typeof ref[key] === "object" && keysAreNumbersAndValuesAreEmptyStrings(ref[key])) {
          ref[key] = Object.keys(ref[key]).map(val => Number(val));
        } else {
          ref = ref[key]
        }
      })
    })
    

完整解决方案:https://jsfiddle.net/s2attatz/1/

var arr = ["data",
  "data.cell",
  "data.cell.celltiming",
  "data.cell.celltiming.0",
  "data.cell.celltiming.1",
  "data.cell.earfcn",
  "data.cell.pci",
  "data.cell.rsrp",
  "data.cell.rsrp.0",
  "data.cell.rsrp.1",
  "data.cell.rsrp.2",
  "data.cell.rsrq",
  "data.cell.rsrq.0",
  "data.cell.rsrq.1",
  "data.cell.rsrq.2",
  "data.cell.sinr",
  "data.cell.sinr.0",
  "data.cell.sinr.1",
  "data.cells",
  "data.cells.0",
  "data.cells.0.ch",
  "data.cells.0.ecno",
  "data.cells.0.r99",
  "data.cells.0.rscp",
  "data.cells.0.sc",
  "data.cells.1",
  "data.cells.1.ch",
  "data.cells.1.ecno",
  "data.cells.1.r99",
  "data.cells.1.rscp",
  "data.cells.1.sc",
  "data.cells.2",
  "data.cells.2.ch",
  "data.cells.2.ecno",
  "data.cells.2.r99",
  "data.cells.2.rscp",
  "data.cells.2.sc",
  "data.cells.3",
  "data.cells.3.ch",
  "data.cells.3.ecno",
  "data.cells.3.r99",
  "data.cells.3.rscp",
  "data.cells.3.sc",
  "data.id",
  "data.mac",
  "data.plmn",
  "data.rssi",
  "data.time",
  "deviceID",
  "time"
]
var obj = arr.reduce((accumulator, currentValue, currentIndex) => {
  let ref = accumulator;
  currentValue.split(".").forEach(key => {
    if (!ref[key]) ref[key] = {}
    ref = ref[key];
  })
  return accumulator;
}, {})

    arr.forEach(chain => { // first pass, change empty objects to empty strings
      let ref = obj;
      chain.split(".").forEach(key => {
        if (Object.keys(ref[key]).length === 0) {
          ref[key] = ''
        } else {
          ref = ref[key]
        }
      })
    })

    function keysAreNumbersAndValuesAreEmptyStrings(object) {
      let entries = Object.entries(object);
      for (let i = 0; i < entries.length; i++) {
        let key = entries[i][0], value = entries[i][1];
        if(isNaN(Number(key)) || typeof value !== "string" || value.length !== 0) return false;
      }
      return true;
    }

    arr.forEach(chain => { // second pass, change objects whose keys are numbers to an array of those keys
      let ref = obj;
      chain.split(".").forEach(key => {
        if (typeof ref[key] === "object" && keysAreNumbersAndValuesAreEmptyStrings(ref[key])) {
          ref[key] = Object.keys(ref[key]).map(val => Number(val));
        } else {
          ref = ref[key]
        }
      })
    })

console.log(JSON.stringify(obj, null, "\t"));
/*
{
  "data": {
    "cell": {
      "celltiming": [
        0,
        1
      ],
      "earfcn": "",
      "pci": "",
      "rsrp": [
        0,
        1,
        2
      ],
      "rsrq": [
        0,
        1,
        2
      ],
      "sinr": [
        0,
        1
      ]
    },
    "cells": {
      "0": {
        "ch": "",
        "ecno": "",
        "r99": "",
        "rscp": "",
        "sc": ""
      },
      "1": {
        "ch": "",
        "ecno": "",
        "r99": "",
        "rscp": "",
        "sc": ""
      },
      "2": {
        "ch": "",
        "ecno": "",
        "r99": "",
        "rscp": "",
        "sc": ""
      },
      "3": {
        "ch": "",
        "ecno": "",
        "r99": "",
        "rscp": "",
        "sc": ""
      }
    },
    "id": "",
    "mac": "",
    "plmn": "",
    "rssi": "",
    "time": ""
  },
  "deviceID": "",
  "time": ""
}
*/

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2021-09-23
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多