Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
agrupar claves de objeto con el mismo valor

tengo un objeto

 var myObject = {"audi": "gas", "tesla": "electric", "bmw": "electric", "mercedes": "electric"}

y quiero agrupar todas las claves con el mismo valor en una cadena como usar solo es5

 tesla bmw mercedes: electric. audi: gas

Usé una combinación de Object.keys y Object.values pero no funcionó para mí

 var res = '' Object.keys(myObject).map(function(key) { Object.values(myObject).map(function(val) { if (myObject[key] === val) { kes = { [res.concat(key)]: val } } }) })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Tendría más sentido agrupar por 'combustible'. Para eso, puedes usar un reductor .

En el fragmento algunos caminos diferentes a Roma...

Ver también...

 const logElem = document.querySelector(`pre`); var myObject = { audi: "gas", tesla: "electric", bmw: "electric", mercedes: "electric" }; const byFuel = Object.entries(myObject) .reduce((acc, [key, value]) => ({ ...acc, [value]: (acc[value] || []).concat(key) }), {}); logElem.textContent = `***By fuel (ES6)***\n ${ JSON.stringify(byFuel, null, 2)}`; // to swap keys and values from [byFuel] const byCars = Object.entries(byFuel) .reduce((acc, [key, value]) => ({ ...acc, [value.join(`,`)]: key }), {}); logElem.textContent += `\n\n***By cars from byFuel (ES6)***\n${ JSON.stringify(byCars, null, 2)}`; // Use one reducer (ES6) const byCarsInOne = Object.entries(myObject) .reduce((acc, [key, value]) => { const keysAggregatedAsSingleKey = Object.keys(myObject) .filter( key => myObject[key] === value ) .join(`,`); return acc[keysAggregatedAsSingleKey] ? acc : { ...acc, [keysAggregatedAsSingleKey]: value }; }, {}); logElem.textContent += `\n\n***One reducer***\n${ JSON.stringify(byCarsInOne, null, 2)}`; // ES5 (really necessary? All modern browsers support es6) var byFuelEs5 = {}; var byCarsEs5 = {}; for (var key in myObject) { var ky = myObject[key]; byFuelEs5[ky] = ky in byFuelEs5 ? byFuelEs5[ky].concat(key) : [key]; } logElem.textContent += `\n\n***ES5***\n${ JSON.stringify(byFuelEs5, null, 2)}`; for (var key in byFuelEs5) { byCarsEs5[byFuelEs5[key].join(`,`)] = key; } logElem.textContent += `\n\n${ JSON.stringify(byCarsEs5, null, 2)}`; // Object.keys ES5, not very efficient // (makes you *really* want to use ES6 ;) var cars = Object.keys(myObject); var byCarsEs5_2 = cars .map( function(key) {return [myObject[key], key]; }) .map( function(kv) { var fuel = kv[0]; var comma = this[fuel] ? "," : ""; this[fuel] = (this[fuel] || "") + comma + kv[1]; return this; }, {} ) .map( function(obj) { var keys = Object.keys(obj) .map( key => this[obj[key]] = key ); return this; }, {}).shift(); logElem.textContent += `\n\n***Object.keys and map ES5***\n${ JSON.stringify(byCarsEs5_2, null, 2)}`; // Finally, reduce from Object.keys my be more efficient var byCarsEs5_3 = cars .reduce( function(acc, car) { var fuel = myObject[car]; if (!~acc.fuels.indexOf(fuel)) { acc.fuels.push(fuel); var carsByFuel = cars.filter(key => myObject[key] === fuel); acc.result[carsByFuel] = fuel; } return acc; }, {fuels: [], result: {}} ).result; logElem.textContent += `\n\n***ES5 one reducer from Object.keys***\n${ JSON.stringify(byCarsEs5_3, null, 2)}`;
 <pre></pre>

about 4 years ago · Juan Pablo Isaza Report

0

¿Funcionaría esto para usted (compatible con IE)?

 var myObject = { "audi": "gas", "tesla": "electric", "bmw": "electric", "mercedes": "electric" }; var reverseObject = Object.keys(myObject).reduce(function(result, car) { var type = myObject[car], list = result[type]; if (!list) result[type] = list = []; list.push(car); return result; }, {}); console.log(reverseObject); var result = Object.keys(reverseObject).map(function(type) { return reverseObject[type].join(" ") + ": " + type; }).join("."); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

 var myObject = {audi: "gas", "tesla": "electric", bmw: "electric", "mercedes": "electric"}; var helper = Object.entries(myObject).reduce((helperObj,item) => { var key = item[0]; var val = item[1]; if(val in helperObj) { helperObj[val].push(key) } else { helperObj[val] = [key]; } return helperObj; }, {}); var res = Object.entries(helper).reduce((theRes,item) => { var fuel = item[0]; var cars = item[1]; var resKey = cars.join(' '); theRes[resKey] = fuel; return theRes; }, {}); console.log(res)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!