iter-object

An object iterator and object replacer with callbacks for key-value pairs

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
iter-object
102.0.04 years ago5 years agoMinified + gzip package size for iter-object in KB

Readme

iter-object
!Downloadslink-download !Versionlink-version !Licenselink-license

An object iterator and object replacer with callbacks for key-value pairs

Installation

npm install iter-object
or
yarn add iter-object

Usage

| Method | Params | Description | |--|--|--| | iterobj | (src: Object, fn: Function ) | Iterate over objects and display key-value pairs | | iterrepl | (src: Object, fn: Function ) => any | Iterate over object and return modified or new values |

iterObject

Example:
import { iter_obj } from 'iter-object';

const src = {
    owner: 'Alex',
    car: {
        make: 'Toyota',
        model: 'Matrix',
        year: 2013
    }
}

iter_obj(src, (key, value) => {
    console.log(key, value);
});

// output:
// owner Alex
// car { make: 'Toyota', model: 'Matrix', year: 2013 }
// make Toyota
// model Matrix
// year 2013

iterReplace

Example:
import { iter_repl } from 'iter-object';

const sellerContent = {
  owner: 'Alex',
  car: {
    make: 'Toyota',
    model: 'Matrix',
    year: 2013,
  },
};

const buyerContent = iter_repl(sellerContent, (key, value) => {
  switch(key) {
      case 'owner': {
          return 'New Buyer';
      }
      case 'car': {
          return { ...value, color: 'White' };
      }
      default: {
          return value;
      }
  }
});

console.log(JSON.stringify(buyerContent, null, 2));

// output:
// {
//   "owner": "New Buyer",
//   "car": {
//     "make": "Toyota",
//     "model": "Matrix",
//     "year": 2013,
//     "color": "White"
//   }