javascript将对象推送到数组对象但其他对象受到影响

时间:2017-05-26 09:41:31

标签: javascript arrays object

我的问题是当我向数组对象添加新对象时,最后一个对象字段会覆盖其他对象字段。最后,所有对象都变得相同。这是示例

array=[{id:1 names:[john,james,alice]},
       {id:2 names:[lisa,carlos,josh]}]

var obj={id:3 names:[david]}

array.push(obj)

console.log(array)

 //=>      [{id:1 names:[david]},
            {id:2 names:[david]},
            {id:3 names:[david]}]

尝试删除其中一个时遇到同样的问题。你有什么建议吗?

5 个答案:

答案 0 :(得分:3)

你缺少一些撇号和昏迷。

array = [
  { id: 1, names: ['john', 'james', 'alice']},
  { id: 2, names: ['lisa', 'carlos', 'josh']}
];

var obj={ id:3, names: ['david']}

array.push(obj)

console.log(array)

答案 1 :(得分:0)

You Js无效。尝试以下内容:



var array =[
  {id:1, names:['john', 'james', 'alice']},
   {id:2, names:['lisa', 'carlos', 'josh']}]

var obj= {id:3, names:['david']}

array.push(obj)

console.log('array => ', array)




答案 2 :(得分:0)

    array=[{id:1, names:["john","james","alice"]},
           {id:2, names:["lisa","carlos","josh"]}];
    
    var obj={id:3, names:["david"]};
    
    array.push(obj);
    
    alert(array[0].names);
    
    alert(array[1].names);
    
    alert(array[2].names);

好像你的代码有点混乱。 这是一个正确的版本

答案 3 :(得分:0)

你必须将字符串放在引号中,否则它将保持未定义。在数组中的id之后缺少所述(,)。请尝试以下代码 -

 var array=[{id:1, names:["john","james","alice"]}, {id:2, names:["lisa","carlos","josh"]}];

        var obj={id:3, names:["david"]};

        array.push(obj);

        for(z=0;z<array.length;z++){

           alert(array[z])  ;

        }

答案 4 :(得分:0)

您的public class FragmentActivity1 extends Fragment { public FragmentActivity1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_fragment_activity1, container, false); } } 不是public class MainActivity extends AppCompatActivity { private ViewPager pager; private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar_hotel_detail); setSupportActionBar(toolbar); ViewCompat.setTransitionName(findViewById(R.id.app_bar_layout), "Extra Image"); CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolBar_hotel_details); collapsingToolbarLayout.setContentScrimColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)); collapsingToolbarLayout.setTitle("Collaps"); pager = (ViewPager) findViewById(R.id.viewPager); ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new FragmentActivity1()); adapter.addFragment(new FragmentActivity2()); adapter.addFragment(new FragmentActivity3()); pager.setAdapter(adapter); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(pager); tabLayout.getTabAt(0).setText("Tab1"); tabLayout.getTabAt(1).setText("Tab2"); tabLayout.getTabAt(2).setText("Tab3"); } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment) { mFragmentList.add(fragment); } } } ,否则您的代码工作正常。

enter image description here

工作演示:

&#13;
&#13;
JSON
&#13;
&#13;
&#13;