Skip to main content

part1 javascript higher order functions


  const companies=[
  {name:"facebook",start:"100",end:"110"}, {name:"amazon",start:"123",end:"321"},{name:"bamboo",start:"34",end:"45"}, {name:"netflix",start:"20",end:"223"}
]
const ages = [31,21,11,3,15,32,53,34,15,76,79,84,91]

//For each
companies.forEach((item)=>{console.log(item["name"] + "..elements")});

output:
facebook :elements
amazon :elements

// filter out ages that are below 21 (remove elements less than 21)

//filter
const canSmoke=ages.filter((age)=>(age>21))
console.log(canSmoke)
const oldestCompany = companies.filter((comp)=>{if (comp.end>100){return true;} })
//console.log("The oldest is: " + JSON.stringify(oldestCompany))
console.log("The oldest is: " + oldestCompany)
oldestCompany.forEach(x=>console.log(x))
const eightiesCompany = companies.filter(
company=> (company.start >=1980 && company.start <1990)
);

// sorting by company start date

const sortedCompanies = companies.sort(
(a,b)=> (a.start > b.start ? 1 : -1)
);
console.log(sortedCompanies)

o/p:-

[{"name":"netflix","start":20,"end":223},{"name":"bamboo","start":34,"end":45},{"name":"facebook","start":100,"end":110},{"name":"amazon","start":123,"end":321}] 

Comments

Popular posts from this blog

part 2 react hooks with objects and array

i mport React, { useState } from 'react'; import './App.css' const App = () => { const [name, setName] = useState({ fname: '', lname: '' }) return ( <div className="App"> <form> <input type="text" value={name.fname} onChange= {(e) => setName((prevstate) => ({ ...prevstate, fname: e.target.value }))} /> Fname is {name.fname} <br /> <input type="text" value={name.lname} onChange= {(e) => setName((prevstate) => ({ ...prevstate, lname: e.target.value }))} /> Lname is {name.lname} </form> { JSON . stringify ( name ) } </div> ); } export default App; onChange= {(e) => setName((prevstate) => ({ ...prevstate, lname: e.target.value }))} the ...prevstate above will merge current state to previous state #working with ARRAY import React , { useState } from 'react' ; import './App.css' const App = () => { const [ items , setItems ] =...

react hooks part 1

. import React , { useState } from 'react' ; import './App.css' const App = () => { const [ count1 , setCount1 ] = useState ( 1 ) const [ count2 , setCount2 ] = useState ( 2 ) const [{ count3 , count4 }, setCount ] = useState ({ count3 : 8 , count4 : 9 }) return ( <div className = "App" > <button onClick = { () => { setCount1 ( c => c + 1 ) setCount2 ( c => c + 1 ) } } > Add-it </button> <div> count1= { count1 } </div> <div> count2= { count2 } </div> <button onClick = { () => { setCount (( c ) => ({ ... c , count3 : count3 + 100 , count4 : count4 + 100 }) ) //here "c" is currentstate and the ...c spreadoperator } } > add obj </button> <div> count3= { count3 } </div><div> count4= { count4 } </div> </div> ); } export def...

part 4, fetch data with useEffect axios

--------------------------------------------------------------------------   import React , { useState , useEffect } from 'react' ; import axios from 'axios' const DataPull = () => { const [ id , setId ] = useState ( 1 ) const [ post , setPost ] = useState ({}) const [ buttonid , setButtonid ] = useState ( 1 ) const handleClick = () => { console . log ( "button id set" ) setButtonid ( id ) } useEffect (() => { console . log ( "normal id " + id ) console . log ( "button id " + buttonid ) axios . get ( `https://jsonplaceholder.typicode.com/posts/ ${ buttonid } ` ). then ( res => { setPost ( res . data ) }). catch ( err => { console . log ( err ) setPost ({ title : "no data" }) }) }, [ buttonid ]) return ( <div> <input type = "text" ...