比较当前的迭代值和先前的迭代值

时间:2019-05-07 07:56:41

标签: javascript ecmascript-6

我有这个对象数组,我想从中建立一个字符串。

 [
   {
     "parent": "mcd",
     "child": "coke"
   },
   {
     "parent": "mcd",
     "child": "burger"
   },
   {
     "parent": "kfc",
     "child": "chicken"
   }
 ]

我该如何生产?

mcd_coke_burger-kfc_chicken

6 个答案:

答案 0 :(得分:0)

我在这里使用pGroup来查看某些“孩子”是否具有相同的父母:

let items = [
   {
     "parent": "mcd",
     "child": "coke"
   },
   {
     "parent": "mcd",
     "child": "burger"
   },
   {
     "parent": "kfc",
     "child": "chicken"
   }
 ];
var str = '';
var pGroup = [];
for (i=0; i<items.length; i++){
  if (pGroup.includes(items[i].parent)) {
    str += '_' + items[i].child;
  }
  else {
    pGroup.push(items[i].parent);
    if (i != 0) {
      str += '-';
    }
    str += items[i].parent + '_' + items[i].child;
  }
}
console.log(str);

答案 1 :(得分:0)

我对您的问题的处理方法如下:

1)通过parent属性对对象数组进行分组。这将导致您使用键值对的对象,并以parent的值作为键。

2)遍历对象,并通过将每个child的值附加到其对应的父对象来形成新的字符串。

const arr = [{
    "parent": "mcd",
    "child": "coke"
  },
  {
    "parent": "mcd",
    "child": "burger"
  },
  {
    "parent": "kfc",
    "child": "chicken"
  }
]

const grouped = arr.reduce((h, obj) => Object.assign(h, {
  [obj['parent']]: (h[obj['parent']] || []).concat(obj)
}), {});

let result = '';
let index = 0;

for (let key in grouped) {
  if (index !== 0) {
    result += '-'
  }
  result += 'key';
  grouped[key].map(obj => {
    result += `_${obj.child}`;
  });
  index++;
}
console.log(result);

答案 2 :(得分:0)

您可以尝试这样:

const data = [
   {
     "parent": "mcd",
     "child": "coke"
   },
   {
     "parent": "mcd",
     "child": "burger"
   },
   {
     "parent": "kfc",
     "child": "chicken"
   }
];

// Group by 'parent' property
const groupedParent = data.reduce((acc, curr) => {
  if(!acc[curr.parent]) 
	acc[curr.parent] = [];
  
  acc[curr.parent].push(curr);
  return acc;
}, {});

// Now do join on the entries
const res = Object.entries(groupedParent)
                  .map(x => `${x[0]}_${x[1].map(y => y.child).join('_')}`)
                  .join('-');

console.log(res);

答案 3 :(得分:0)

假设您的数组排序正确。

const items = [
    {
        "parent": "mcd",
        "child": "coke"
    },
    {
        "parent": "mcd",
        "child": "burger"
    },
    {
        "parent": "kfc",
        "child": "chicken"
    }
];

let currentParent;
let string = '';
items.forEach((item, i) => {
    if (item.parent !== currentParent) {
        currentParent = item.parent;
        string += (i ? '-' : '') + currentParent;
    }
    string += '_' + item.child;
});
console.log(string);

答案 4 :(得分:0)

将项目分组,然后将各组串联:

const order = [{
    "parent": "mcd",
    "child": "coke"
  },
  {
    "parent": "mcd",
    "child": "burger"
  },
  {
    "parent": "kfc",
    "child": "chicken"
  }
];

const grouped = order.reduce((grouped, item) => {
  grouped[item.parent] = grouped[item.parent] || [];
  grouped[item.parent].push(item.child);
  return grouped;
}, {});

const result = Object.keys(grouped)
    .map(key => `${key}_${grouped[key].join('_')}`)
    .join('-');

console.log(result);

答案 5 :(得分:0)

首先将值进行相应的分组-然后只需将值连接起来即可:

const data = [{"parent":"mcd","child":"coke"},{"parent":"mcd","child":"burger"},{"parent":"kfc","child":"chicken"}];
const res = Object.entries(data.reduce((acc, { parent, child }) => {
  acc[parent] = acc[parent] || [];
  acc[parent].push(child);
  return acc;
}, {})).map(([k, v]) => `${k}-${v.join("_")}`).join("_");
console.log(res);