1

let say i have 1 multidimensional array and i want to exclude values that not equal in javascript.

here is the example array.

var filter = ["big_number", "odds_number"];
var arrays = {
    "first" : {
        "big_number" : [50,51,52],
        "odds_number" : [39,41,51,53]
    },
    "second" : {
        "big_number" : [61,62,63,64,65,70,72,73],
        "odds_number" : [13,15,17,19,61,63,65,73]
    }
};

i want to convert that array to be like this.

var new_arrays = {
    "first" : [51],
    "second" : [61,63,65,73]
};

here is my code

var newArray = {
    "first" : [],
    "second" : []
};
for (var k in arrays){
    if (arrays.hasOwnProperty(k)) {
        for(var f=0; f<filter.length; f++) {
            newArray[k].push(arrays[k][filter[f]].filter(value => -1 !== arrays[k][filter[f]].indexOf(value))));
        }
   }
}
console.log(newArray);

actually i could do this code

var newArray = {
    "first" : [],
    "second" : []
};
for (var k in arrays){
    if (arrays.hasOwnProperty(k)) {
        newArray[k].push(arrays[k]["big_number"].filter(value => -1 !== arrays[k]["odds_number"].indexOf(value))));
    }
}
console.log(newArray);

but i need to convert it through filter variable. i could not use filter[0] and filter[1], because that values could change dynamically and could be more than 2 values in array.

5
  • why down vote my question? Commented Feb 20, 2019 at 10:58
  • I guess that's because you haven't shown any attempt yourself. And I personally don't see the pattern here. Commented Feb 20, 2019 at 11:12
  • @JonasWilms i have mentioned that i could done it if there were 2 arrays separated.. so should i need to put code with 2 arrays separated? Commented Feb 20, 2019 at 11:14
  • @FZs could you read the question first before you said it duplicate question? Commented Feb 20, 2019 at 11:45
  • @FZs is it clear for you for that updated question? Commented Feb 20, 2019 at 12:05

4 Answers 4

2

You could loop through the keys and update the values using filter and includes:

var arrays={"first":{"big_number":[50,51,52],"odds_number":[39,41,51,53]},"second":{"big_number":[61,62,63,64,65,70,72,73],"odds_number":[13,15,17,19,61,63,65,73]}};

for (let key in arrays) {
  arrays[key] = arrays[key]["big_number"]
                  .filter(n => arrays[key]["odds_number"].includes(n));
}

console.log(arrays)

If you don't want to mutate the original object then use Object.entries and reduce:

var arrays={"first":{"big_number":[50,51,52],"odds_number":[39,41,51,53]},"second":{"big_number":[61,62,63,64,65,70,72,73],"odds_number":[13,15,17,19,61,63,65,73]}};

const newObject = Object.entries(arrays).reduce((r, [key, {big_number, odds_number}]) => {
  r[key] = big_number.filter(n => odds_number.includes(n));
  return r
}, {})

console.log(newObject)

If you have more than 2 array properties, you can do something like this: Get all the arrays using Object.values and then use reduce to run the previous code recursively

var arrays = {
  "first": {
    "big_number": [50, 51, 52],
    "odds_number": [39, 41, 51, 53],
    "another_key": [41, 51, 53]
  },
  "second": {
    "big_number": [61, 62, 63, 64, 65, 70, 72, 73],
    "odds_number": [13, 15, 17, 19, 61, 63, 65, 73],
    "another_key": [63, 65]
  }
};

for (let key in arrays) {
  arrays[key] = Object.values(arrays[key])
                       .reduce((a, b) => a.filter(c => b.includes(c)))
}

console.log(arrays)

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

12 Comments

sorry, i forget to put filter variable.. it should loop through that variable.. i have update the question..
What do you mean by filter variable? Instead of arrays[key]["big_number"], do arrays[key][filter[0]] and filter[1] for odds_number
yes.. filter variable value is ["big_number", "odds_number"] and the variable could change dynamically through checkbox.. so i want to loop it through that filter variable..
Do as mentioned in the previous comment.
@WillyantoHalim Intersection is getting items common to all arrays. If one of them is empty, then result will be empty array. This is valid
|
1

Here is a little intersection snippet:

function intersect(a,b){
    b.slice()
    return a.filter(item=>{
        if(b.includes(item)){
            b.splice(b.indexOf(item),1)
            return true
        }
    })
}

Using that, you can do this easily:

function intersect(a,b){
	b.slice()
	return a.filter(item=>{
		if(b.includes(item)){
			b.splice(b.indexOf(item),1)
			return true
		}
	})
}

var filter = ["big_number", "odds_number"];
var output={}
var arrays = {
    "first" : {
        "big_number" : [50,51,52],
        "odds_number" : [39,41,51,53]
    },
    "second" : {
        "big_number" : [61,62,63,64,65,70,72,73],
        "odds_number" : [13,15,17,19,61,63,65,73]
    }
};
for(x in arrays){
    output[x]=arrays[x][filter[0]]
    for(let i=1;i<filter.length;i++){
        output[x]=intersect(output[x],arrays[x][filter[i]])
    }
}
console.log (output) 

2 Comments

you can have a look for the updated question and remove that duplicate question tag.. thanks
@WillyantoHalim I updated my answer to be dynamic. And sorry for the duplicate mark, I can't remove that, but only you can see; simply ignore it. Happy coding!
0

use Object.entries to get keys and values and then use reduce

var arrays = {
  "first" : {
      "big_number" : [50,51,52],
      "odds_number" : [39,41,51,53]
  },
  "second" : {
      "big_number" : [61,62,63,64,65,70,72,73],
      "odds_number" : [13,15,17,19,61,63,65,73]
  }
};

const output =Object.entries(arrays).reduce((accu, [key, {big_number}]) => {
  if(!accu[key]) accu[key] = [];
  big_number.forEach(num => {
    if(num%2 !==0)
    accu[key].push(num);
  })
  return accu;
}, {});

console.log(output);

1 Comment

sorry, i forget to put filter variable.. it should loop through that variable.. i have update the question.
0

You can get the unique values from both the arrays using Set and then using filter get only the common values.

var arrays = {"first": {"big_number": [50, 51, 52],"odds_number": [39, 41, 51, 53]},"second": {"big_number": [61, 62, 63, 64, 65, 70, 72, 73],"odds_number": [13, 15, 17, 19, 61, 63, 65, 73]}},
    result = Object.keys(arrays).reduce((r,k) => {
      let setB = new Set(arrays[k]["big_number"]);
      r[k] = [...new Set(arrays[k]["odds_number"])].filter(x => setB.has(x));
      return r;
    },{});
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

sorry, i forget to put filter variable.. it should loop through that variable.. i have update the question.

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.