Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

later useful #6

Open
mqy1023 opened this issue Oct 8, 2016 · 0 comments
Open

later useful #6

mqy1023 opened this issue Oct 8, 2016 · 0 comments

Comments

@mqy1023
Copy link
Owner

mqy1023 commented Oct 8, 2016

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant