js操作对象和数组对象 取keyvalue

JavaScript071

js操作对象和数组对象 取keyvalue,第1张

var people = [

  { "name": "bob", "dinner": "pizza" },

  { "name": "john", "dinner": "sushi" },

  { "name": "larry", "dinner": "hummus" }

]

people.filter(function (person) { 

  if(person.dinner){

    console.log(person.dinner)

  }

  if(person.name){

    console.log(person.name)

  }

})

var people1 = { "name": "bob", "dinner": "pizza" }

Object.keys(people1)

console.log(Object.keys(people1))

var jsonObj = {"1" : 50,"2" : 40,"3" : 40,"4" : 30,"5" : 20,"6" : 20,"7" : 20,"8" : 20,"9" : 20, "10" : 20}

var keyAry = []

var valueAry = []

for(var key in jsonObj){

  keyAry.push(key)

  valueAry.push(jsonObj[key])

}

console.log(keyAry)

console.log(valueAry)

php数组根据某一个键值,把相同键值的合并生成一个新的二维数组

源数据:

$infos = array( array( 'a' =>36, 'b' =>'xa', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>3, 'b' =>'vd', 'c' =>'2015-08-20 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>6, 'b' =>'wwe', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>36, 'b' =>'se', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>6, 'b' =>'aw', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>36, 'b' =>'bv', 'c' =>'2015-08-28 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

), array( 'a' =>12, 'b' =>'xx', 'c' =>'2015-08-27 00:00:00', 'd' =>'2015/08/438488a00b3219929282e3652061c2e3.png'

)

)1234567891011121314151617181920212223242526272829303132333435363738394041424344

处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组

代码:

<?php

$result= array()foreach ($infos as $key =>$info) { $result[$info['a']][] = $info

}

print_r($result)123456

输出:

Array(

[36] =>Array

(

[0] =>Array

(

[a] =>36

[b] =>xa

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[1] =>Array

(

[a] =>36

[b] =>se

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[2] =>Array

(

[a] =>36

[b] =>bv

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[3] =>Array

(

[0] =>Array

(

[a] =>3

[b] =>vd

[c] =>2015-08-20 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[6] =>Array

(

[0] =>Array

(

[a] =>6

[b] =>wwe

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

[1] =>Array

(

[a] =>6

[b] =>aw

[c] =>2015-08-28 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

[12] =>Array

(

[0] =>Array

(

[a] =>12

[b] =>xx

[c] =>2015-08-27 00:00:00

[d] =>2015/08/438488a00b3219929282e3652061c2e3.png

)

)

)