admin管理员组

文章数量:1287958

Here, I have one object and one array I want to filter value paring both array and object,

object have multiple values see in below code Reader, Author, Publisher.., in array Author and Reader I want to pare them and want this type of results of wanted result :- [1, 8] this is my object

object1 = { 1: "Reader"
  8: "Author"
  3: "Publisher"
  9: "Site Editor"
  11: "Guest"
  12: "Editor"
  13: "Designer"
  14: "Publicist"
}

this is my array

array1 = ["Reader", "Author"]

Here, I have one object and one array I want to filter value paring both array and object,

object have multiple values see in below code Reader, Author, Publisher.., in array Author and Reader I want to pare them and want this type of results of wanted result :- [1, 8] this is my object

object1 = { 1: "Reader"
  8: "Author"
  3: "Publisher"
  9: "Site Editor"
  11: "Guest"
  12: "Editor"
  13: "Designer"
  14: "Publicist"
}

this is my array

array1 = ["Reader", "Author"]
Share Improve this question edited May 8, 2019 at 11:15 Dharmesh asked May 8, 2019 at 11:12 DharmeshDharmesh 6,00318 gold badges49 silver badges67 bronze badges 2
  • 1 please add the wanted result as well. ... and what you have tried. – Nina Scholz Commented May 8, 2019 at 11:13
  • @NinaScholz I update my question – Dharmesh Commented May 8, 2019 at 11:16
Add a ment  | 

6 Answers 6

Reset to default 3

You could filter the keys of the object and check if the key has a value in the array using includes

const object1 = {
  1: "Reader",
  8: "Author",
  3: "Publisher",
  9: "Site Editor",
  11: "Guest",
  12: "Editor",
  13: "Designer",
  14: "Publicist"
};

const array1 = ["Reader", "Author"]

const keys = Object.keys(object1).filter(k => array1.includes(object1[k]))

console.log(keys)

You could take a Map and return the values by using the switched key/value from the entries of the object.

The result are strings, because the keys of an object are either strings or symbols.

var object = { 1: "Reader", 8: "Author", 3: "Publisher", 9: "Site Editor", 11: "Guest", 12: "Editor", 13: "Designer", 14: "Publicist" },
    array = ["Reader", "Author"],
    result = array.map(
        Map.prototype.get,
        new Map(Object.entries(object).map(([k, v]) => [v, k]))
    );

console.log(result);

I think this solution might help you

 var values = Object.keys(object1).filter(key => {
   return array1.indexOf(object1[key]);
 });
console.log(values);

This will work ,with array filter ,map and find.

object1 = { 1: "Reader",
  8: "Author",
  3: "Publisher",
  9: "Site Editor",
  11: "Guest",
  12: "Editor",
  13: "Designer",
  14: "Publicist",
}


array1 = ["Reader", "Author"]

let result=Object.values(object1)

let finalresult=array1.filter(ele=>result.includes(ele)).map(element=>Object.keys(object1).find(key => object1[key] === element))
console.log(finalresult)

You can loop over the object and check if the value is present in the array1. If so then push the key to an array

let obj = {
  1: "Reader",
  8: "Author",
  3: "Publisher",
  9: "Site Editor",
  11: "Guest",
  12: "Editor",
  13: "Designer",
  14: "Publicist",
}

let array1 = ["Reader", "Author"];
let newArry = [];

for (let keys in obj) {
  if (array1.includes(obj[keys])) {
    newArry.push(keys)
  }
}
console.log(newArry)

I think the below code can be helpful for you :

array1=[object1.1,object1.2]

the property object 1 is not array it is an object and each object has a property and value here in object1 '1' is a property and 'Reader' is value of this property and you can get each value of this object by dot and the name of property.

本文标签: How to filter values from array and object comparison in javascriptStack Overflow