我正在尝试从JSON数组中删除一段数据。例如,我有这个数组;
var users = [];
io.on('connection',function(socket){
socket.on('nickname', function(nick){
socket.nick = nick;
users.push({
user:socket.nick,
userid:socket.id,
socket:socket
});
});
我试图以这种方式删除用户;
socket.on('disconnect', function(){
delete users[{
user,
userid,
socket
}];
});
});
我该怎么做?
答案 0 :(得分:3)
您可以尝试使用lodash方法来实现您的目标: -
// Contact Form
add_shortcode('contact_form', 'theme_shortcode_contact_form');
function theme_shortcode_contact_form($atts, $content = null, $code) {
extract(shortcode_atts(array(
'mail_to' => '',
'form_id' => false,
'button_color' => 'black'
), $atts));
$id = mt_rand(0, 1000);
$template_directory = get_template_directory_uri();
$ajax_url = admin_url('admin-ajax.php');
$mail_to = str_replace('@','[at]',$mail_to);
$name_text = __('Name', 'theme_front');
$email_text = __('Email', 'theme_front');
$phonenumber_text = __('Phone Number', 'theme_front');
$address_text = __('Property Address', 'theme_front');
$message_text = __('Message', 'theme_front');
$submit_text = __('Submit', 'theme_front');
$form_id = ( $form_id ) ? '<input type="hidden" name="form-id" value="' . $form_id . '" />' : '';
return <<<RET
[raw]
<form method="post" action="$ajax_url" id="form-$id" class="contact-form ajax-form validate-form">
<div class="form-input-item clearfix">
<label for="contact-form-$id-contact-name">$name_text <span class="required-star">*</span></label>
<input type="text" class="input-text" name="contact-name-h" id="contact-form-contact-name-h">
<input type="text" tabindex="8" size="22" value="" class="input-text {required:true}" name="contact-name" id="contact-form-$id-contact-name">
<div class="form-error-box"></div>
</div>
<div class="form-input-item clearfix">
<label for="contact-form-$id-contact-email">$email_text <span class="required-star">*</span></label>
<input type="text" tabindex="9" size="22" value="" class="input-text {required:true, email:true}" name="contact-email" id="contact-form-$id-contact-email">
<div class="form-error-box"></div>
</div>
<div class="form-input-item clearfix">
<label for="contact-form-$id-contact-phonenumber">$phonenumber_text <span class="required-star">*</span></label>
<input type="text" tabindex="9" size="22" value="" class="input-text {required:true}" name="contact-phonenumber" id="contact-form-$id-contact-phonenumber">
<div class="form-error-box"></div>
</div>
<div class="form-input-item clearfix">
<label for="contact-form-$id-contact-address">$address_text <span class="required-star">*</span></label>
<input type="text" tabindex="9" size="22" value="" class="input-text {required:true}" name="contact-address" id="contact-form-$id-contact-address">
<div class="form-error-box"></div>
</div>
<div class="form-input-item clearfix">
<label for="contact-form-$id-contact-content">$message_text <span class="required-star">*</span></label>
<textarea tabindex="10" rows="3" cols="30" class="input-textarea {required:true}" id="contact-form-$id-contact-content" name="contact-content"></textarea>
<div class="form-error-box"></div>
</div>
<div class="form-response"></div>
<div class="form-input-item form-input-item-last clearfix">
<button class="button medium $button_color" type="submit"><span>$submit_text</span></button>
</div>
$form_id
<input type="hidden" name="mail_to" value="$mail_to">
<input type="hidden" name="action" value="do_contact" />
</form>
看看这是否有效..
答案 1 :(得分:0)
如果您知道每个用户对象的每个属性的值,您可以将值传递给函数并循环通过users数组以查找匹配的对象,然后将其从数组中拼接(更新的答案通过在对象中起作用):
var users = [];
for (var i = 1; i < 10; i++) {
users.push({
user: i,
userId: i,
socket: i
});
}
/**
* Loop through the users array to find matching
* object to remove
**/
var deleteUser = function(userToDelete) {
for (var userObj in users) {
var userObject = users[userObj];
if (typeof userObject === "object" &&
userObject.user === userToDelete.user &&
userObject.userId === userToDelete.userId &&
userObject.socket === userToDelete.socket) {
users.splice(userObj, 1);
break;
}
}
console.log(users);
};
deleteUser({
user: 2,
userId: 2,
socket: 2
});
&#13;