JS json 배열 다시 가져오기

1322 단어 js
        /**
	*   json         
	*/
	function transArrayByName(array,name){
		let rarray = [];
		array.map(function(a){
			if(typeof a == 'object'){
				if(a[name]){
					rarray.push(a[name]);
				}
			}
		})
		return rarray;
	}
	/*
	*	    ,  name   json  
	*/
	function uniqueArray(array,name){
		let rarray = [];
		let tjson = {};
		array.map(function(a){
			if(typeof a == 'object'){
				if(!tjson[a[name]]){
					tjson[a[name]] = true;
					rarray.push(a);
				}
			} else {
				if(!tjson[a]){
					tjson[a] = true;
					rarray.push(a);
				}
			}
		})
		return rarray;
	}
let array = [{name:'1',age:22},{name:'2',age:23},{name:'3',age:22},{name:'4',age:25}]
let array2 = [1,2,3,'5','4','5',1,3]

transArrayByName(array,'age')
//[22, 23, 22, 25]

transArrayByName(array,'name')
//["1", "2", "3", "4"]

uniqueArray(array,'name')
//[{name: "1", age: 22},{name: "2", age: 23},{name: "3", age: 22},{name: "4", age: 25}]

uniqueArray(array,'age')
/*
[{name: "1", age: 22}
{name: "2", age: 23}
{name: "4", age: 25}]
*/

uniqueArray(array2)
//[1, 2, 3, "5", "4"]

좋은 웹페이지 즐겨찾기