首页 > 代码库 > tojson

tojson

const toJSON = ( ...args ) => ( item ) => {
const result = {};

for( let i = 0; i < args.length; i ++ ){
result[ args[ i ] ] = item[ i ];
}

return result;
}
undefined
var array = [ [ ‘jiaoyu‘, 1000 ], [ ‘tongyong‘, 2000 ] ];
undefined
array.map( toJSON( ‘hangye‘, ‘shouru‘ ) );
(2) [Object, Object]
JSON.string;
undefined
JSON.stringify( array.map( toJSON( ‘hangye‘, ‘shouru‘ ) ) );
"[{"hangye":"jiaoyu","shouru":1000},{"hangye":"tongyong","shouru":2000}]"
JSON.stringify( array.map( toJSON( ‘shouru‘, ‘hangye‘ ) ) );
"[{"shouru":"jiaoyu","hangye":1000},{"shouru":"tongyong","hangye":2000}]"

tojson