react-with-es6 icon indicating copy to clipboard operation
react-with-es6 copied to clipboard

later useful

Open mqy1023 opened this issue 8 years ago • 0 comments

1、bind inside

this.props.products.forEach(function(product) {
      if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
        return;
      }
      if (product.category !== lastCategory) {
        rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
      }
      rows.push(<ProductRow product={product} key={product.name} />);
      lastCategory = product.category;
    }.bind(this));

2、reduce

const glypyMapMaker = (glypy) => Object.keys(glypy).map((key) => {
  return {
    key,
    value: String.fromCharCode(parseInt(glypy[key], 16))
  };
}).reduce((map, glypy) => {
  map[glypy.key] = glypy.value
  return map;
}, {});

export {
  glypyMapMaker
};
/**
 * [a,b,c]==> {a:'a',b:'b',c:'c'}
 * @param obj
 * @return {*}
 */
module.exports=function(obj){
   return obj.reduce(function (pre,cur) {
       pre[cur]=cur;
       return pre;
   },{})
};

3、fetch

fetch(fetchUrl, {method: 'GET' })
    .then( (response) => response.json() )
    .then( (responseText)  => {
            /* *** */
    })
    .catch((e)=>{
            /* *** */
    });

4、merged two objects into one

// small helper function which merged two objects into one
function MergeRecursive(obj1, obj2) {
  for (var p in obj2) {
    try {
      if ( obj2[p].constructor==Object ) {
        obj1[p] = MergeRecursive(obj1[p], obj2[p]);
      } else {
        obj1[p] = obj2[p];
      }
    } catch(e) {
      obj1[p] = obj2[p];
    }
  }
  return obj1;
}

mqy1023 avatar Oct 08 '16 03:10 mqy1023