0

I have the following json which is an array with 2 nested arrays:

[
  [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
          {
            "name": "WalkRight",
            "frameRate": 15
          },
          {
            "name": "Laughing",
            "frameRate": 15
          },
          {
            "name": "Megaphone",
            "frameRate": 15
          },
          {
            "name": "Yawning",
            "frameRate": 15
          }
        ],
        [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
          {
            "name": "WalkRight",
            "frameRate": 15
          }
        ]
]

I need to make a new array of the common object names seen in both arrays like this:

[{
            "name": "WalkBack"
          },
          {
            "name": "WalkFront"
          },
          {
            "name": "WalkLeft"
          },
          {
            "name": "WalkRight"
          }]

So in this case the walk animations are the common names. I tried using:

result = firstArray.filter(o => secondArray.some(({name}) => o.name === name));

but this only works if you know have names for the inner arrays. Any ideas?

1
  • I answered but please tell me if I understood your problem correctly Commented Jun 6, 2020 at 11:58

4 Answers 4

1

Simplest would be to use the reduce method on them by taking first array as a reference. Something like this:

const data=[[{name:"WalkBack",frameRate:15},{name:"WalkFront",frameRate:15},{name:"WalkLeft",frameRate:15},{name:"WalkRight",frameRate:15},{name:"Laughing",frameRate:15},{name:"Megaphone",frameRate:15},{name:"Yawning",frameRate:15}],[{name:"WalkBack",frameRate:15},{name:"WalkFront",frameRate:15},{name:"WalkLeft",frameRate:15},{name:"WalkRight",frameRate:15}]];

const [refArr, ...rest] = data

const formattedData = refArr.reduce((commons = [], item) => {
  let isCommon = rest.every(arr => arr.some(itemInArr => item.name === itemInArr.name))
  return isCommon ? [...commons, item] : commons
}, [])

console.log(formattedData)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer but that's still including the uncommon object names such as "Laughing" and "Megaphone".
Sorry about that. I have updated my answer. It will work on any size of an array.
Thanks, would this still work if there are more than 2 inner arrays?
It is also cross-browser compatible since it uses only reduce, some, and every prototype methods. If you try to use flat to flatten them first and then process it, it would break in IE (unless you are poly filling). But, I would rather prefer what is available than adding more code to the bundle. :)
1

First you can flat your data then you reduce it and filter out records by checking length, if it has more than one value.

var data=[ [ { "name": "WalkBack", "frameRate": 15 }, { "name": "WalkFront", "frameRate": 15 }, { "name": "WalkLeft", "frameRate": 15 }, { "name": "WalkRight", "frameRate": 15 }, { "name": "Laughing", "frameRate": 15 }, { "name": "Megaphone", "frameRate": 15 }, { "name": "Yawning", "frameRate": 15 } ], [ { "name": "WalkBack", "frameRate": 15 }, { "name": "WalkFront", "frameRate": 15 }, { "name": "WalkLeft", "frameRate": 15 }, { "name": "WalkRight", "frameRate": 15 } ]];

var result2 =  Object.entries(data.flat().reduce((acc, {name})=>{
   acc[name] = [...(acc[name] || []),{name}];
   return acc;
},{})).filter(([k,v])=>v.length>1).map(([name])=>({name}));

console.log(result2);

4 Comments

Thanks for your answer but this is somewhat hard-coded to know that there are always 2 arrays. It could be 2 or more. Anyway to automate it more?
@AndrewJuniorHoward, I have edited my answer let me know if this helps you.
Thanks I'm just having trouble using .flat(). I'm just trying to get my typescript to allow it.
Perfect! Thank you so much, there's no way I would have guessed all that :)
0

First you gather all the "names", then you filter the duplicates :)

var arrays = [
  [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
          {
            "name": "WalkRight",
            "frameRate": 15
          },
          {
            "name": "Laughing",
            "frameRate": 15
          },
          {
            "name": "Megaphone",
            "frameRate": 15
          },
          {
            "name": "Yawning",
            "frameRate": 15
          }
        ],
        [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
          {
            "name": "WalkRight",
            "frameRate": 15
          }
        ]
]

var names = []
arrays.forEach(function(array) {
  array.forEach(function(item) {
    names.push(item.name)
  })
})

var result = []
names.filter( (x,i,a) => a.slice(0,i).find(y=>y==x)  ).forEach(x=>result.push({name: x}))
console.log(result)

Comments

0

var arr = [
  [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
          {
            "name": "WalkRight",
            "frameRate": 15
          },
          {
            "name": "Laughing",
            "frameRate": 15
          },
          {
            "name": "Megaphone",
            "frameRate": 15
          },
          {
            "name": "Yawning",
            "frameRate": 15
          }
        ],
        [
          {
            "name": "WalkBack",
            "frameRate": 15
          },
          {
            "name": "WalkFront",
            "frameRate": 15
          },
          {
            "name": "WalkLeft",
            "frameRate": 15
          },
            {
            "name": "WalkRight",
            "frameRate": 15
          }
        ]
];
debugger;
var arr1 = arr[0], arr2 = arr[1],
myObj = {};
arr1.forEach(i=>{
 if(!myObj[i.name]){
   myObj[i.name] = true;
 }
}
);

arr2.forEach(i=>{
 if(!myObj[i.name]){
   delete myObj[i.name]
 }
})

console.log(myObj)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.