“Objektfilter” Code-Antworten

Kann Filter für Objekte in JavaScript verwendet werden

var heroes = [
	{name: “Batman”, franchise: “DC”},
	{name: “Ironman”, franchise: “Marvel”},
	{name: “Thor”, franchise: “Marvel”},
	{name: “Superman”, franchise: “DC”}
];

var marvelHeroes =  heroes.filter(function(hero) {
	return hero.franchise == “Marvel”;
});

// [ {name: “Ironman”, franchise: “Marvel”}, {name: “Thor”, franchise: “Marvel”} ]
Bright Beaver

Filter JavaScript Array

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
White Browed Owl

JavaScript -Objektfilter

function objectFilter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => (res[key] = obj[key], res), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};

var filtered = objectFilter(scores, num => num > 1); 
console.log(filtered);
QuietHumility

.filter js

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
Difficult Dolphin

Filter JavaScript

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const filter = arr.filter((number) => number > 5);
console.log(filter); // [6, 7, 8, 9]

or 

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
Aggressive Albatross

Objektfilter

const numWins = {
  'BUF': 11,
  'MIA': 9,
  'NE': 6,
  'NYJ': 1
};

const atLeast9Wins = _.flow([
  Object.entries,
  arr => arr.filter(([key, value]) => value >= 9),
  Object.fromEntries
])(numWins);

atLeast9Wins; // { BUF: 11, MIA: 9 }
Jolly Jaguar

Ähnliche Antworten wie “Objektfilter”

Fragen ähnlich wie “Objektfilter”

Weitere verwandte Antworten zu “Objektfilter” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen