This page is not yet available in Spanish. We are working on its translation. If you have any questions or feedback about our current translation project, feel free to reach out to us!
Metadata
ID:javascript-best-practices/no-dupe-keys
Language: JavaScript
Severity: Error
Category: Error Prone
Description
Object literals should not have duplicate keys. If you define an object with duplicate keys, the last one will overwrite any preceding ones.
Non-Compliant Code Examples
varx={a:b,['a']:b};varx={y:1,y:2};varx={'':1,'':2};varx={'':1,[``]:2};varfoo={0x1:1,1:2};// should be captured by no-octal
// var x = { 012: 1, 10: 2 };
varx={0b1:1,1:2};varx={0o1:1,1:2};varx={1n:1,1:2};varx={1_0:1,10:2};varx={"z":1,z:2};varfoo={bar:1,bar:1,bar(){}}varx={a:1,get['a'](){}};varx={a:1,seta(value){}};varx={a:1,b:{a:2},getb(){}};varx=({'/(?<zero>0)/':1,[/(?<zero>0)/]:2})// while we can't evaluate the value of the template string we will assume that
// two keys with the exact same template string will evaluate to the same key
constobj={props:{[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},}}
Compliant Code Examples
varfoo={__proto__:1,two:2};varx={foo:1,bar:2};varx={'':1,bar:2};varx={'':1,' ':2};varx={'':1,[null]:2};varx={'':1,[a]:2};varx={[a]:1,[a]:2};+{geta(){},seta(b){}};varx={a:b,[a]:b};varx={a:b,...c}varx={geta(){},seta(value){}};varx={a:1,b:{a:2}};varx=({null:1,[/(?<zero>0)/]:2})var{a,a}=obj// should be captured by no-octal
// var x = { 012: 1, 12: 2 };
varx={1_0:1,1:2};// template literals should be valid as long as they are not exactly the same
constobj={props:{[`${classes.foo}${classes.bar}`]:():boolean=>{returnnull;},[`${classes.baz}${classes.bla}`]:():boolean=>{returnnull;},}}
Integraciones sin problemas. Prueba Datadog Code Security
Datadog Code Security
Prueba esta regla y analiza tu código con Datadog Code Security
Cómo usar esta regla
1
2
rulesets:- javascript-best-practices # Rules to enforce JavaScript best practices.
Crea un static-analysis.datadog.yml con el contenido anterior en la raíz de tu repositorio
Utiliza nuestros complementos del IDE gratuitos o añade análisis de Code Security a tus pipelines de CI.