Vektramani ob'ekt xususiyatiga olib tashlang

Ob'ekt xususiyatini olganda verguldan qutilishga harakat qilaman, lekin men uni olganda men uni har doim olaman. Men buni sinab ko'rdim:

var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];

var getName =  items.map(function(element){
        if (element.id == 2){
        return element.name.replace(',', '');
    }
});

alert(getName);

Va men buni doimo olaman

enter image description here

yoki bu

enter image description here

Here is FIDDLE

Xo'sh, qanday qilib biron-bir vergulsiz toza nomni chop etish mumkin?

0
Nomlarda vergul yo'q
qo'shib qo'ydi muallif Weedoze, manba
OK, qanday o'zgaruvchini vergul qoldirmasdan chop etish mumkin, faqat nom?
qo'shib qo'ydi muallif lewis4u, manba
Barchangizga rahmat, hozir juda ko'p narsani o'rgandim.
qo'shib qo'ydi muallif lewis4u, manba
Qiymati vergul yo'q. Sizning muammoingiz map() qatorni yaratganligi va siz alert() alert() bo'lsa, u qatorni mag'lubiyatga keltirganligidir. alert() kabi bir qatorda har doim ham majburiy emas, balki o'zgaruvchining turi o'zgaruvchining turi sifatida uning o'rniga console.log yordamida barcha xatolarni tuzatishni taklif qilaman. qiladi
qo'shib qo'ydi muallif Rory McCrossan, manba

6 javoblar

Array # map() faqat ismlarni olish uchun Array # filter() dan foydalanishingiz mumkin va join() Nomzodlarni ko'rsatish uchun natijaga qo'shilish uchun

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];
var neededId = "1";
console.log(items.filter(i=>i.id===neededId).map(i=>i.name).join(' '));
</div> </div>

vergulni tushuntirish

You are creating a new array getName which will contain 2 values. The name ball but also an undefined value. Array#map() is iterating over all the array but for the second object with id=2 you return nothing. Thus it become undefined

console.log dan foydalanishda aniqlanmagan qiymatni ko'rishingiz mumkin.

alert() will display an empty string instead of undefined. That's why you see the comma. It is to separate the 2 strings

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",       "name": "ball" }, {     "id": "2", "name": "galaxy" }];

var getName =  items.map(function(element){
                if (element.id == 1){
        return element.name.replace(',', '');
    }
});
console.log(getName);
</div> </div>
1
qo'shib qo'ydi
@ lewis4u Bajarildi!
qo'shib qo'ydi muallif Weedoze, manba
@Toma Ushbu funktsiyani bilaman, lekin OPda uning haqiqiy holatida turli xil ob'ektlar mavjudligini tasavvur qila olmayman
qo'shib qo'ydi muallif Weedoze, manba
Rahmat qilaman, lekin sizning javobingizda barcha nomlarni chop etaman va faqat ID ning ma'lum bir raqamiga ega bo'lishim kerak, agar siz buni to'g'irlashingiz mumkin bo'lsa, javobingiz potensial eng yaxshi javobdir!
qo'shib qo'ydi muallif lewis4u, manba
@Weedoze, chiqish Array # find() kabi, var ism = items.find (funktsiya (item) {return item.id === 1}). .
qo'shib qo'ydi muallif Thomas, manba
    var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];

    var getName =  items.map(function(element){
        if (element.id == 1){          
            return element.name.replace(',', '');
        }
    }).join('');

    alert(getName);   

Fiddle havolasi

1
qo'shib qo'ydi

Natijani filtrlash va istalgan mulkni xaritaga solishingiz mumkin, chunki Array # map barcha elementlar uchun biror narsalarni qaytaradi, lekin Array # filter bitta natija to'plamini olish uchun.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];

var getName =  items.filter(function (element) {
            return element.id !== "2";
        }).map(function(element){
            return element.name;
        }).join(', ');

console.log(getName);
</div> </div>
1
qo'shib qo'ydi

Array # toString() qanday ishlaydi: null va undefined qiymatlari bo'sh satrlarga aylanadi va Array " , ":

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var names = [undefined, "galaxy"];

console.log("names (array):", names);
console.log("names (string):", String(names));
</div> </div>

alert(names) forces names to be converted to String, because alert() can only print strings. Better use the console.

Men sizning nomingiz yoki barcha nomlarni chop qilmoqchi bo'lganingizdan amin emasman, sizning kodingiz menga juda moyil.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];


//since you're using `map` it implies that you want 
//to fetch the name of every item in the array
//because this is what `Array#map()` does.
function getAllNames(){
  return items.map(item => item.name);
}

console.log("all names:", getAllNames(items));

//on the other hand, your condition would imply you want only a specific item
function getNameById(id){
  return items.find(item => item.id === id).name;
}

console.log("name of #1:", getNameById("1"));
console.log("name of #2:", getNameById("2"));
</div> </div>

Yana bir narsa bor. Siz o'zingizning o'zgaruvchini getName deb nomladingiz, u menga ism olish uchun bajaradigan vazifani o'z ichiga olishi kerakligini anglatadi. Lekin bunday emas. Unda nomlar va undefined qiymatlari mavjud.

Argumentlar va funktsiyalarning to'g'ri nomlanishi kodning muayyan qismida nima sodir bo'lishini tushunishga yordam beradi; faqatgina biz uchun emas, balki ikki hafta ichida ushbu kodni yana bir bor ko'rib chiqishingiz kerak.

1
qo'shib qo'ydi
Bu eng yaxshi javob, deb aytishim kerak!
qo'shib qo'ydi muallif lewis4u, manba

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",       "name": "ball" }, {     "id": "2", "name": "galaxy" }];

$.each(items, function(e, val) {
 alert(val.name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</div> </div>
1
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var items = [{ "id": "1",   "name": "ball" }, { "id": "2", "name": "galaxy" }];

// map function will return array of data
// from the code below, we will get
// names = ['ball', 'galaxy']
var names = items.map(function(element){
  return element.name;
});

// to print all names without comma,
// simply join each array item with ' '
console.log(names.join(' '));//print >> ball galaxy

// if you want to select particular name, using find() may be more sense
const ball = items.find(function(item) {
  return item.id === '1';
});

console.log(ball.name);//print >> ball
</div> </div>
1
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb