繁体   English   中英

JS:用另一个对象的数据附加对象数组

[英]JS: append array of objects with data from another

我有一些 JS 数据以各种不同的结构保存各种数据、数字、子对象、数组等:

let datapile = {
  cover_img: { uid:'u2a3j4' },
  avatar_img: { uid:'u5j3vg' },
  created: 8273736384,
  friends: [
    { name:'John', img: { uid:'u2726b' }, },
    { name:'Jane', parent: { profile_img: { uid:'u293k4' }, } },
  ],
  occupation: {
    past: current,
    prior: {
      title: 'Accountant',
      company: {
        logo: { img: { uid:'u29374' } },
      }
    },
  },
  ...
}

然后我得到了这个 JS 图像列表:

let imgs : [
  { uid:'u2a3j4', format:'jpg', alt_txt:'Lorem...', size:583729, dominant_color:'#d79273' },
  { uid:'u5j3vg', format:'png', alt_txt:'Lorem...', size:284849, dominant_color:'#f99383' },
  { uid:'u2726b', format:'gif', alt_txt:'Lorem...', size:293742, dominant_color:'#349a83' },
  ...
],

现在,我需要的是一个我可以调用的函数,它将查看数据堆并从下面的 imgs 列表中附加 img 数据对象。 所以数据堆现在只有 uid 引用,它应该有整个 img 对象。 然后我将对所有其他类型的引用数据做同样的事情。

我尝试了以下功能:

function isArray(x){ return ( x !== undefined && Array.isArray(x) ) }
function isObject(x){ return (x && typeof x === "object" && !Array.isArray(x)) }
function get_item(type, uid) { /* loops through eg. imgs and returns img matching uid */ }

function append_referenced_relations(data){
    if( !data ) return data

    if( isObject(data) && data['uid'] !== undefined ) {
        let item = get_item('any', data['uid'])
        data = item
    }

    if( isObject(data) || isArray(data) ) {
        for( let key in data ) {
            data[key] = this.append_referenced_relations(deepClone(data[key]))
        }
    }

    return data
}

...但我就是无法让它工作。 我对类似情况的最佳谷歌搜索也一无所获。 互联网可以帮助我吗?

你可以试试这样的

基本上它使用递归和Object.fromEntries /entries来检查内部对象的所有键

如果您有任何具体问题,请随时问我

 const decorate = (obj, data) => { if (typeof obj !== 'object') { return obj } if (Array.isArray(obj)) { return obj.map(e => decorate(e, data)) } return Object.fromEntries( Object.entries(obj).flatMap(([k, v]) => { if (k === 'uid') { const imgData = data.find(d => v === d.uid) return Object.entries(imgData || [[k, v]]) } return [ [k, decorate(v, data)] ] }) ) } let datapile = { cover_img: { uid: 'u2a3j4' }, avatar_img: { uid: 'u5j3vg' }, created: 8273736384, friends: [{ name: 'John', img: { uid: 'u2726b' }, }, { name: 'Jane', parent: { profile_img: { uid: 'u293k4' }, } }, ], occupation: { past: 'current', prior: { title: 'Accountant', company: { logo: { img: { uid: 'u29374' } } } } } } let imgs = [{ uid: 'u2a3j4', format: 'jpg', alt_txt: 'Lorem...', size: 583729, dominant_color: '#d79273' }, { uid: 'u5j3vg', format: 'png', alt_txt: 'Lorem...', size: 284849, dominant_color: '#f99383' }, { uid: 'u2726b', format: 'gif', alt_txt: 'Lorem...', size: 293742, dominant_color: '#349a83' } ] console.log(decorate(datapile, imgs))

您需要在嵌套datapile递归以识别具有uid的对象并添加要添加的img属性。

需要考虑的几个案例:

  1. 对象。 (如果对象具有uid属性,则停止对其属性的递归)
  2. 具有对象的对象值。
  3. 对象数组。

实际上不需要在函数中返回任何地方,因为我们可以内联更新对象。

尝试如下。

 let imgs = [ { uid: "u2a3j4", format: "jpg", alt_txt: "Lorem...", size: 583729, dominant_color: "#d79273", }, { uid: "u5j3vg", format: "png", alt_txt: "Lorem...", size: 284849, dominant_color: "#f99383", }, { uid: "u2726b", format: "gif", alt_txt: "Lorem...", size: 293742, dominant_color: "#349a83", }, { uid: "u293k4", format: "gif", alt_txt: "Lorem...", size: 193742, dominant_color: "#349a83", }, { uid: "u29374", format: "gif", alt_txt: "Lorem...", size: 793742, dominant_color: "#349a83", }, ]; let datapile = { cover_img: { uid: "u2a3j4" }, avatar_img: { uid: "u5j3vg" }, created: 8273736384, friends: [ { name: "John", img: { uid: "u2726b" } }, { name: "Jane", parent: { profile_img: { uid: "u293k4" } } }, ], occupation: { past: "current", prior: { title: "Accountant", company: { logo: { img: { uid: "u29374" } }, }, }, }, }; function isArray(x) { return x !== undefined && Array.isArray(x); } function isObject(x) { return typeof x === "object" && !Array.isArray(x); } function get_item(uid) { return imgs.find((img) => img.uid === uid); } function append_referenced_relations(data) { if (isObject(data)) { if (data["uid"] !== undefined) { const img = get_item(data.uid); // Add img properties to the same object as properties Object.entries(img).forEach(([key, value]) => { data[key] = value; }); } else { // Recurse for the object values Object.values(data).forEach((item) => { append_referenced_relations(item); }); } } else if (isArray(data)) { data.forEach((item) => { // Recurse for the array entries append_referenced_relations(item); }); } } append_referenced_relations(datapile); console.log(JSON.stringify(datapile, null, 2));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM