根据以下数据创建自定义对象

时间:2019-02-07 16:27:14

标签: javascript

嗨,我正在尝试从海关最终对象(代码示例)中使用对象以及对象和其他对象组成的数组。我试图在数组上执行forEach并创建一个新对象,并将其推送到最终数组,但没有运气。 snapObj中的数字可能不是对象dataArr中对象的第一个数字(digit0可能是digit1 ...),这使我很难找到正确的数字并根据需要进行表示。有什么建议吗?

var dataArr = {};

//First array of objects
var arrayContactsWithNames = [{
  "digits0": "5555648583",
  "digits1": "4155553695",
  "name": "Kate Bell",
}, {
 "digits0": "5554787672",
 "digits1": "4085555270",
 "digits2": "4085553514",
 "name": "Daniel Higgins Jr.",
}, {
  "digits0": "8885555512",
  "digits1": "8885551212",
  "name": "John Appleseed",
}, {
  "digits0": "5555228243",
  "name": "Anna Haro",
}, {
  "digits0": "5557664823",
  "digits1": "7075551854",
  "name": "Hank M. Zakroff",
}, {
 "digits0": "5556106679",
 "name": "David Taylor",
}];

// Second object
var snapObj = {
  "5556106679": "test 1",
  "7075551854": "test 2",
  "8885551212": "test 1"
};


/* This is what I want to get:

var dataArr = [{
  "test 1": {
    "5556106679": "David Taylor",
    "8885551212": "John Appleseed"
  }
}, {
  "test 2": Object {
    "7075551854": "Hank M. Zakroff",
  }
}]
*/


// This is what I tried
arrayContactsWithNames.forEach((i) => {
  if (i in snapObject) {
     const newObj = Object.assign({snapObject[i],{ name },{digits${i}}});
     dataArr.push(newObj);
  }
});

     
    

2 个答案:

答案 0 :(得分:3)

逻辑是首先创建number:name形式的对象字典,并将其用于从第二个对象中查找。这是最佳选择,因为只需要O(1)即可在对象中查找键。

var arrayContactsWithNames = [{
  "digits0": "5555648583",
  "digits1": "4155553695",
  "name": "Kate Bell",
}, {
 "digits0": "5554787672",
 "digits1": "4085555270",
 "digits2": "4085553514",
 "name": "Daniel Higgins Jr.",
}, {
  "digits0": "8885555512",
  "digits1": "8885551212",
  "name": "John Appleseed",
}, {
  "digits0": "5555228243",
  "name": "Anna Haro",
}, {
  "digits0": "5557664823",
  "digits1": "7075551854",
  "name": "Hank M. Zakroff",
}, {
 "digits0": "5556106679",
 "name": "David Taylor",
}];

var snapObj = {
  "5556106679": "test 1",
  "7075551854": "test 2",
  "8885551212": "test 1"
};

let parsedArr = arrayContactsWithNames.reduce((acc,{name,...digits}) => {
  Object.values(digits).forEach(digit => { acc[digit] = name });
  return acc;
}, {});  // create a { number : name } mapping with some es6 magic. Refer Object destructuring and spread operator to know how the digit0,digit1 etc are extracted. 
// console.log(parsedArr) 

let parsedObj = Object.keys(snapObj).reduce((acc,num) => { 
  let val = snapObj[num]
  if(!acc[val])
    acc[val] = {}
  acc[val][num] = parsedArr[num]; // use the lookup object with the number
  return acc;
},{}); // A simple reducer to create a new object in the desired shape.

console.log(parsedObj);

答案 1 :(得分:1)

如果我正确理解了您想要的内容,请考虑以下示例:

const data = {}

const snap = {
	'5556106679': 'test 1',
	'7075551854': 'test 2',
	'8885551212': 'test 1'
}

const objects = [
{
	'digits0': '5555648583',
	'digits1': '4155553695',
	'name': 'Kate Bell',
},
{
	'digits0': '5554787672',
	'digits1': '4085555270',
	'digits2': '4085553514',
	'name': 'Daniel Higgins Jr.',
},
{
	'digits0': '8885555512',
	'digits1': '8885551212',
	'name': 'John Appleseed',
},
{
	'digits0': '5555228243',
	'name': 'Anna Haro',
},
{
	'digits0': '5557664823',
	'digits1': '7075551854',
	'name': 'Hank M. Zakroff',
},
{
	'digits0': '5556106679',
	'name': 'David Taylor',
}]

for (const key in snap)
{
	if (!data[snap[key]])
	{
		data[snap[key]] = {}
	}
	
	for (const object of objects)
	{
		for (const i in object)
		{
			if (object[i] === key)
			{
				data[snap[key]][key] = object.name
				break
			}
		}
	}
}

document.querySelector('textarea').value = JSON.stringify(data, null, '  ')
<textarea cols="50" rows="10"></textarea>