Object.fromEntries

Exemples

const cours = { html: 12, css: 2, JS: 30 };

Object.entries(cours)

> Array [Array ["html", 12], Array ["css", 2], Array ["JS", 30]]

Object.fromEntries(Object.entries(cours))

> Object { html: 12, css: 2, JS: 30 }


En action

const cours = { html: 12, css: 2, JS: 30 };
const  choix = Object.fromEntries(
  Object.entries(cours)
  .filter(([ _, val ]) => val>10 )
);
console.log(choix); // > Object { html: 12, JS: 30 }

En action

const words = ["Beau", "cool", "Ange", "Ananas", "ballon"]; const alpha = words.reduce((a, x) => { const l = x[0].toUpperCase() if(!a[l]) a[l] = []; a[l].push(x); return a; }, {}); const glosaire = Object.fromEntries( Object.entries(alpha) .sort(([a] , [b]) => a<b?-1:1)); console.log(glosaire); //
  • [object Object] {
      A
    : ["Ange", "Ananas"],
      B
    : ["Beau", "ballon"],
      C
    : ["cool"]
    }

regexp

 function normalizeDataKey(key) {

    return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`);
  }

  console.log(normalizeDataKey("denisDupont")) // denis-dupont

destructuration, map

const arr = [{

    id: 1,
    name: 'bill'
  }, {
    id: 2,
    name: 'ted'
  }]
  
const result = arr.map( ({id:value,name:text}) => ({ value, text }));
  console.log(result)

` `

 function test(lit,...sub){

    console.log(lit);
    console.log(sub);

}
const nom = "dupont"
const adj = "cool"

test `denis ${nom} est ${adj}`;

modules

 index.html

src

    index.js

    query.js


index.html

<!doctype html>
<html>
<head>
    <title>My project</title>
</head>
<body>
    <script src="./src/index.js" type="module"></script>
</body>
</html>

index.js

import retrieveContent from './query.js';

console.log("here they are");
async function showContent() {
  try {
    const content = await retrieveContent();
    let ul = document.createElement('ul');
    content.forEach(element => {
       let li = document.createElement('li');
       li.innerText = element;
       ul.appendChild(li);

    });
   
    

    document.getElementsByTagName('body')[0].appendChild(ul);
  } catch (e) {
    console.log('Error', e);
  }
}

showContent();

query.js


export default async function retrieveContent() {
    const url = "https://baconipsum.com/api/?type=all-meat&paras=2&start-with-lorem=1";
  
    const response = await fetch(url);
    return response.json();
  }
  


before/after

/* On ajoute un coeur avant les liens */
a::before {
  content: "♥";
}

 lien

Server nodejs

sous powershell 

> $env:PORT="8000"    

> echo $env:PORT      

8000

const http = require('http')

const port =  process.env.PORT;

const server = http.createServer((req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/html')
  res.end('<h1>Hello, World!</h1>')
})

server.listen(port, () => {
  console.log(`Server running at port ${port}`)
})
In Windows Command-Prompt the syntax is 
>set PORT="8000"  
>echo %POST% 
 To get a list of all environment variables enter the command set To send those variables to a text file enter the command set > filename.txt