问题描述
我真的不知道如何问这个问题,但希望解释有所帮助。
简单地说,有两个嵌套数组。
oldArray 看起来像:
oldArray = [
0:{attributes:[0,0],name:"Variant Red/Long"},1:{attributes:[0,1],name:"Variant Red/Short"},2:{attributes:[1,name:"Variant Blue/Long"},3:{attributes:[1,name:"Variant Blue/Short"},]
和 newArray 如下:
newArray = [
0:{attributes:[0,0]},1]},]
两者都是从另一个包含属性的数组生成的。 oldArray 是使用每个属性值的索引生成的:
oldAttributes = [
0:{values:["red","blue"]},1:{values:["long","short"]},]
newArray 根据以下属性计算:
newAttributes = [
0:{values:["red"]},]
如您所见,Blue 属性已被移除。从 newAttributes 重新创建 newArray 很简单,那里没有问题。问题在于已分配给 oldArray 的名称,需要将其添加到 newArray 的适用索引中。
在上面的场景中,属性 Blue 被删除了,所以任何不影响 Blue 的东西都应该被添加回 newArray,如下所示:
newArray = [
0:{attributes:[0,name:"Variant Red/Short},]
我没有重新创建数组并重新添加那些名称对象,而是尝试想出一种方法来仅删除那些受旧数组影响的对象,但无法提出一个与它的顺序完全相同的数组属性:
[0,0]
[0,1]
这个案例只有两个不同的属性,颜色和长度。我们的真实案例最多使用三个属性。
for (let i = 0; i < newVariants.length; i++){
for (let j = 0; j < newVariants[i].attributes.length; j++){
for (let a = 0; a < oldVariants.length; a ++){
for (let b = 0; b < oldVarian
ts[a].attributes.length; b++){
if (newVariants[i].attributes[j] === oldVariants[a].attributes[b]){
const tempAttributes = newVariants[i].attributes;
newVariants[i] = oldVariants[a];
newVariants[i].attributes = tempAttributes;
}
}
}
}
}
但大脑无法再跟踪循环。 至于只是删除和索引,我确实有一些代码,但它很混乱,根本无法正常工作。
有没有人对如何处理这个特殊案例有任何指示? 非常感谢任何帮助。
解决方法
我已经做到了...
const strCap1=s=>`${s.charAt(0).toUpperCase()}${s.slice(1).toLowerCase()}`
function setNewArray( arr,arrAtt )
{
let attN = arrAtt.map(e=>e.values.length),arr2 = arr.filter(e=>e.attributes.every((n,i)=>n<attN[i]))
;
arr2.forEach((e,x,t)=>t[x].name = 'Variant '
+ e.attributes
.map((v,i)=>strCap1(arrAtt[i].values[v]))
.join('/'))
return arr2
}
const
oldArray =
[ { attributes: [ 0,0 ],name: 'Variant Red/Long' },{ attributes: [ 0,1 ],name: 'Variant Red/Short' },{ attributes: [ 1,name: 'Variant Blue/Long' },name: 'Variant Blue/Short' }
],newAttributes =
[ { values: [ 'red' ] },{ values: [ 'long','short' ] }
],newArray =
setNewArray(oldArray,newAttributes)
;
console.log(newArray)
.as-console-wrapper { max-height: 100% !important; top: 0; }
.as-console-row { background-color: #87f1f1; }
.as-console-row::after { display:none !important; }
这真的很乱,只是乱涂乱画的结果,但下面的代码非常适合我的用例。以后我会想办法浓缩它。
const addRemainingVariants = (oldVariants,newVariants,attributeIndex,deletedIndex) => {
let removed = false; //set flag when attribute is remove
for (let i = 0; i < newVariants.length; i++){
if (oldVariants[0].attributes.length === newVariants[i].attributes.length && !removed){
if (oldVariants[0].attributes.length === 1){ //only 1 attribute,shift down after deleted index
for(let j = 0; j < oldVariants.length; j++){
if (oldVariants[j].attributes[attributeIndex] < deletedIndex){
if (oldVariants[j].attributes[0] === newVariants[i].attributes[0]){
newVariants[i] = oldVariants[j];
}
} else {
if ((oldVariants[j].attributes[attributeIndex] > deletedIndex)){
if (oldVariants[j].attributes[0] - 1 === newVariants[i].attributes[0]){ //above deleted index,so substract 1
newVariants[i] = oldVariants[j];
newVariants[i].attributes[attributeIndex] = newVariants[i].attributes[attributeIndex] - 1 //reduce index
}
}
}
}
}
if (oldVariants[0].attributes.length === 2){ //two attributes
for(let j = 0; j < oldVariants.length; j++){
if (attributeIndex === 0){
if (oldVariants[j].attributes[0] > deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] - 1 && newVariants[i].attributes[1] === oldVariants[j].attributes[1]){
newVariants[i] = oldVariants[j];
newVariants[i].attributes[0] = newVariants[i].attributes[0] - 1;
}
}
}
if (attributeIndex === 1){
if (oldVariants[j].attributes[1] > deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] && newVariants[i].attributes[1] === oldVariants[j].attributes[1] - 1){
newVariants[i] = oldVariants[j];
newVariants[i].attributes[1] = newVariants[i].attributes[1] - 1;
}
}
}
if (oldVariants[j].attributes[attributeIndex] < deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] && newVariants[i].attributes[1] === oldVariants[j].attributes[1]){
newVariants[i] = oldVariants[j];
}
}
}
}
if (oldVariants[0].attributes.length === 3){ //three attributes
for(let j = 0; j < oldVariants.length; j++){
if (attributeIndex === 0){
if (oldVariants[j].attributes[0] > deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] - 1 && newVariants[i].attributes[1] === oldVariants[j].attributes[1] && newVariants[i].attributes[2] === oldVariants[j].attributes[2]){
newVariants[i] = oldVariants[j];
newVariants[i].attributes[0] = newVariants[i].attributes[0] - 1;
}
}
}
if (attributeIndex === 1){
if (oldVariants[j].attributes[1] > deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] && newVariants[i].attributes[1] === oldVariants[j].attributes[1] - 1 && newVariants[i].attributes[2] === oldVariants[j].attributes[2]){
newVariants[i] = oldVariants[j];
newVariants[i].attributes[1] = newVariants[i].attributes[1] - 1;
}
}
}
if (attributeIndex === 2){
if (oldVariants[j].attributes[2] > deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] && newVariants[i].attributes[1] === oldVariants[j].attributes[1] && newVariants[i].attributes[2] === oldVariants[j].attributes[2] - 1){
newVariants[i] = oldVariants[j];
newVariants[i].attributes[2] = newVariants[i].attributes[2] - 1;
}
}
}
if (oldVariants[j].attributes[attributeIndex] < deletedIndex){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0] && newVariants[i].attributes[1] === oldVariants[j].attributes[1]){
newVariants[i] = oldVariants[j];
}
}
}
}
} else {
//length has changed
if (newVariants[i].attributes.length === 1){ //changed from two to 1
for(let j = 0; j < oldVariants.length; j++){
if (attributeIndex === 0){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[1]){ //works for attributeindex of 0
newVariants[i] = oldVariants[j];
newVariants[i].attributes.splice(0,1); //drop second index from array
removed = true;
}
}
if (attributeIndex === 1){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0]){ //works for attributeindex of 1
newVariants[i] = oldVariants[j];
newVariants[i].attributes.splice(1,1); //drop second index from array
removed = true;
}
}
}
}
if (newVariants[i].attributes.length === 2){ //change from three to two
for(let j = 0; j < oldVariants.length; j++){
if (attributeIndex === 0){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[1]){ //works for attributeindex of 0
newVariants[i] = oldVariants[j];
newVariants[i].attributes.splice(0,1); //drop second index from array
removed = true;
}
}
if (attributeIndex === 1 || attributeIndex === 2){
if (newVariants[i].attributes[0] === oldVariants[j].attributes[0]){ //works for attributeindex of 1
newVariants[i] = oldVariants[j];
newVariants[i].attributes.splice(attributeIndex,1); //drop second index from array
removed = true;
}
}
}
}
}
}
return newVariants;
}